Make fields readonly if not current user

1.6k views Asked by At

I am using the best in place gem and I want for the fields on a users profile that is not logged in (not current user) to be in readonly mode. ATM a user can be logged in, visit another users page and change their profile options (it won't save to the database). It should be setup so that if you're not the current user then all profile information will be readonly.

show.html.erb:

<p>Education: <%= best_in_place @user, :education, nil: 'What is your education level?', :type => :select, :collection => [["High school", "High school"], ["Some college", "Some college"], ["Undergraduate", "Undergraduate"], ["Bachelor's", "Bachelor's"], ["Master's", "Master's"], ["PhD", "PhD"], ["Business school", "Business school"], ["Law school", "Law school"], ["Medical school", "Medical school"]] %></p>

Users controller:

   def update
      @user = if current_user.has_role?(:admin)
            User.find(params[:id])
          else
            current_user
          end
         @user.update_attributes(params[:user])
         respond_with @user
        end

 def edit
      @user = User.find(params[:id])
end
1

There are 1 answers

3
Alex Lynham On BEST ANSWER

I'm not sure about what the best in place gem does, but for a regular form, you'd need to add the readonly: true option into the fields in the view. I assumed that your edit view also provides the @user instance variable to the view.

Something like:

<% if current_user.id == @user.id %>
  <p>Education: <%= best_in_place @user, :education, nil: 'What is your education level?', :type => :select, :collection => [["High school", "High school"], ["Some college", "Some college"], ["Undergraduate", "Undergraduate"], ["Bachelor's", "Bachelor's"], ["Master's", "Master's"], ["PhD", "PhD"], ["Business school", "Business school"], ["Law school", "Law school"], ["Medical school", "Medical school"]] %></p>
<% else %>
  <p>Education: <%= best_in_place @user, :education, nil: 'What is your education level?', :type => :select, :collection => [["High school", "High school"], ["Some college", "Some college"], ["Undergraduate", "Undergraduate"], ["Bachelor's", "Bachelor's"], ["Master's", "Master's"], ["PhD", "PhD"], ["Business school", "Business school"], ["Law school", "Law school"], ["Medical school", "Medical school"]], readonly: true %></p>
<% end %>

Hope that helps.

EDIT:

It seems that normal Rails helper options aren't available, so try adding the HTML disabled attribute directly, like so:

<% if current_user.id == @user.id %>
  <p>Education: <%= best_in_place @user, :education, nil: 'What is your education level?', :type => :select, :collection => [["High school", "High school"], ["Some college", "Some college"], ["Undergraduate", "Undergraduate"], ["Bachelor's", "Bachelor's"], ["Master's", "Master's"], ["PhD", "PhD"], ["Business school", "Business school"], ["Law school", "Law school"], ["Medical school", "Medical school"]] %></p>
<% else %>
  <p>Education: <%= best_in_place @user, :education, nil: 'What is your education level?', :type => :select, :collection => [["High school", "High school"], ["Some college", "Some college"], ["Undergraduate", "Undergraduate"], ["Bachelor's", "Bachelor's"], ["Master's", "Master's"], ["PhD", "PhD"], ["Business school", "Business school"], ["Law school", "Law school"], ["Medical school", "Medical school"]], :html_attrs => {:disabled => true} %></p>
<% end %>