我不确定最好的 gem 做了什么,但是对于常规表单,您需要将readonly: true
选项添加到视图中的字段中。我假设您的edit
视图还为视图提供了@user
实例变量。
就像是:
<% 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 %>
希望有帮助。
编辑:
似乎普通的 Rails 辅助选项不可用,所以尝试直接添加 HTML disabled 属性,如下所示:
<% 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 %>