我用restful_authentication
建立了一个用户模型,然后有一个属于它的配置文件模型。我一直在使用fields_for
将编辑用户和用户配置文件的字段组合到用户编辑视图中。
我希望能够在用户显示视图中设置几个字段来使用in_place_editing
插件。它在用户表字段上运行良好,如所给出的示例。
users_controller.rb
in_place_edit_for :user, :email
/视图/用户/show.html.erb
<%= in_place_editor_field :user, :email %>
但是,我不知道如何正确地为编辑视图访问的任何字段在视图上写入控制器或in_place_editor_field
位:
<% fields_for @up do |profile_fields| %>
<%= profile_fields.text_field :status %>
<% end %>
在users_controller中(为了清晰起见):
def edit
@user = User.find(params[:id])
@up = @user.profile
end
如何为类似于:user.profile, :status
的users_controller和/views/users/how.html.erb创建符号?
谢谢你的帮助。
发布于 2009-03-16 05:17:20
我使用in_place_editor助手标记。将要在span/div中编辑的内容包装起来,给它一个唯一的id,并在in_place_editor标记中引用它。
# in the view
<span id="user_email"><%= user.email -%></span>
<%= in_place_editor "user_email", :url => {:controller => :users, :action => 'set_user_email', :id => user} %>
# in the controller
User.content_columns.each do |column|
in_place_edit_for :user, column.name
end
神奇的是:set_user_email --它是在控制器中自动生成的方法,当您在模型及其属性上的控制器中使用in_place_edit_for时,就会创建该方法。
我已经包含了一种允许在模型的所有字段上进行编辑的方法,但是您可能希望将其限制在几个模型属性上。
根据控制器设置的方式,可能需要设置protect_from_forgery以排除set操作。这将从就地编辑字段中删除对post方法的csrf验证。
编辑
处理如何包含真实性令牌。
https://stackoverflow.com/questions/647452
复制相似问题