简体   繁体   English

如何在Rails中以嵌套形式更新子记录

[英]How to update child record in nested form in rails

How would I update a collection of child 'Product' rows when submitting the form below. 提交以下表单时,我将如何更新子“产品”行的集合。

Many thanks 非常感谢

class User < ActiveRecord::Base                                                                                                                                                                             
   has_many :products, :class_name => 'Product', :inverse_of => :user                                                                                                                                       
   accepts_nested_attributes_for :products                                                                                                                                                                  
end                                                                                                                                                                                                         

class Product < ActiveRecord::Base                                                                                                                                                                          
    belongs_to :user, :inverse_of => :products                                                                                                                                                          
end   

The View 风景

<%= form_for(@user) do |f| %>                                                                                                                                                                               

  <div class="field">                                                                                                                                                                                       
    <%= f.label :name %><br>                                                                                                                                                                                
    <%= f.text_field :name %>                                                                                                                                                                               
  </div>                                                                                                                                                                                                    
  <%= f.fields_for :products do |builder| %>                                                                                                                                                                
      <% builder.text_field :name %>                                                                                                                                                                    
  <% end %>                                                                                                                                                                                                 
  <div class="actions">                                                                                                                                                                                     
    <%= f.submit %>                                                                                                                                                                                         
  </div>                                                                                                                                                                                                    
<% end %>                                                                                                                                                                                                   

Here is somewhat minimal update action: 这是一些最小的update操作:

# app/controller/users_controller.rb

def update 
  # Get the user in param
  @user = User.find(params[:id])

  # Update attributes
  if @user.update_attribute(params[:user]) 
    # Redirect to :show for this user.  
    redirect_to :show
  else 
    # update_attriubtes failed.  Render the edit view. 
    render :edit
  end
end

Secondly, you also need to ensure the attributes can be mass assigned such that the call to @user.update_attributes succeeds. 其次,您还需要确保可以批量分配属性, @user.update_attributes成功调用@user.update_attributes Since you've not tagged your question with the appropriate Rails version, following is what you can do for Rails 3 and Rails 4: 由于您尚未使用适当的Rails版本标记问题,因此可以对Rails 3和Rails 4进行以下操作:

  1. Rails 3 - Define each attribute you want to mass assign as attr_accessible Rails 3-将要批量分配的每个属性定义为attr_accessible
  2. Rails 4 - Permit parameters in controllers params.require(:user).permit(...) . Rails 4-控制器params.require(:user).permit(...) Permit参数。

Note that in your view you are not outputting the result of the helper text_field on this line: 请注意,在您的视图中,您不会在此行上输出helper text_field的结果:

<% builder.text_field :name %> 

You need to use <%=...%> (note the equals = sign) in order to output the result of expressions within <%=...%> . 您需要使用<%=...%> (请注意等于=符号)才能输出<%=...%>的表达式结果。 This might have just been a typo here as you've done this correctly on all other elements. 这可能只是这里的错字,因为您已正确处理了所有其他元素。 Replace the line with: 将行替换为:

<%= builder.text_field :name %>

Also, suggest reading the " Action Controller Overview " of guides for details on Controllers. 另外,建议阅读指南的“ 动作控制器概述 ”以获取有关控制器的详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM