如何在同一个 Rails form_for 中更新两个用户对象?
How can I update two User objects in the same Rails form_for?
我正在尝试使用 rails 中的 form_for 同时更新用户模型中的两个对象。原因是一个人可以为自己和配偶(两个不同的用户)更新基本信息,而无需填写两个单独的表格。
我当前更新一个用户的代码如下所示:
<%= form_for(@user, :url=> wizard_path, :method => :put) do |f| %>
<div class="form-group form-question-group">
<%= f.label :state, "What state do you live in?", class: "form-question" %>
<%= f.select(:state, options_for_select(us_states, selected: @user.state), {:prompt => "State"}, :class => "form-control", :required => true) %>
<%= f.hidden_field :user_id, value: @user.id %>
</div>
<div class="form-buttons">
<%= link_to "Back", previous_wizard_path, :class => "align-left" %>
<%= f.submit "Continue", :class => "btn btn-primary align-right" %>
</div>
<% end %>
如何编辑此表单,以便在提交时不仅更新当前用户,还更新用户的配偶(可以通过@user.spouse访问)。
我的想法是使用您的用户控制器更新方法
def update
if @user.update_attributes(user_params)
# here you update spouse
spouse = @user.spouse
if spouse.present?
spouse.state = @user.state
spouse.save
end
flash[:success] = 'success'
end
end
这听起来像是 ActiveRecord::NestedAttributes 的经典用例。
Nested attributes allow you to save attributes on associated records
through the parent. By default nested attribute updating is turned off
and you can enable it using the accepts_nested_attributes_for
class
method. When you enable nested attributes an attribute writer is
defined on the model.
class User
belongs_to :spouse
accepts_nested_attributes_for :spouse
end
<%= form_for(@user, :url=> wizard_path, :method => :put) do |f| %>
# ...
<%= f.fields_for :spouse do |sf| %>
<%= sf.label :name, 'Your spouses name' %>
<%= sf.text_field :name %>
<% end %
<% end %>
def user_params
params.require(:user)
.permit(:foo, :bar, :baz, spouse_attributes: [:id, :name, :email]) # ...
end
由于这是在模型层处理的,因此除了将嵌套属性列入白名单之外,您无需在控制器中执行任何操作。
我正在尝试使用 rails 中的 form_for 同时更新用户模型中的两个对象。原因是一个人可以为自己和配偶(两个不同的用户)更新基本信息,而无需填写两个单独的表格。
我当前更新一个用户的代码如下所示:
<%= form_for(@user, :url=> wizard_path, :method => :put) do |f| %>
<div class="form-group form-question-group">
<%= f.label :state, "What state do you live in?", class: "form-question" %>
<%= f.select(:state, options_for_select(us_states, selected: @user.state), {:prompt => "State"}, :class => "form-control", :required => true) %>
<%= f.hidden_field :user_id, value: @user.id %>
</div>
<div class="form-buttons">
<%= link_to "Back", previous_wizard_path, :class => "align-left" %>
<%= f.submit "Continue", :class => "btn btn-primary align-right" %>
</div>
<% end %>
如何编辑此表单,以便在提交时不仅更新当前用户,还更新用户的配偶(可以通过@user.spouse访问)。
我的想法是使用您的用户控制器更新方法
def update
if @user.update_attributes(user_params)
# here you update spouse
spouse = @user.spouse
if spouse.present?
spouse.state = @user.state
spouse.save
end
flash[:success] = 'success'
end
end
这听起来像是 ActiveRecord::NestedAttributes 的经典用例。
Nested attributes allow you to save attributes on associated records through the parent. By default nested attribute updating is turned off and you can enable it using the
accepts_nested_attributes_for
class method. When you enable nested attributes an attribute writer is defined on the model.
class User
belongs_to :spouse
accepts_nested_attributes_for :spouse
end
<%= form_for(@user, :url=> wizard_path, :method => :put) do |f| %>
# ...
<%= f.fields_for :spouse do |sf| %>
<%= sf.label :name, 'Your spouses name' %>
<%= sf.text_field :name %>
<% end %
<% end %>
def user_params
params.require(:user)
.permit(:foo, :bar, :baz, spouse_attributes: [:id, :name, :email]) # ...
end
由于这是在模型层处理的,因此除了将嵌套属性列入白名单之外,您无需在控制器中执行任何操作。