我正在使用最好的宝石,我希望用户配置文件中未登录(非当前用户)的字段处于只读模式。 ATM用户可以登录,访问其他用户页面并更改其配置文件选项(它不会保存到数据库中)。它应该设置为如果您不是当前用户,那么所有配置文件信息都将是只读的。
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>
用户控制器:
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
答案 0 :(得分:1)
我不确定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禁用属性,如下所示:
<% 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 %>