使用嵌套表单更新来自不同表的两个记录

时间:2014-07-29 16:07:47

标签: ruby-on-rails activerecord ruby-on-rails-4

我的表单正在更新测试人员记录(最终用户)。我还试图更新许多属于测试人员的applied_program记录。

我将参数发送到控制器但无法让它更新applied_program记录。它试图使用所有参数而不仅仅是应用程序参数来更新它。

形式:

<%= form_for @tester, url: { controller: :admins, action: :update_tester }  do |f| %>
        <%= render 'shared/error_messages' %>

        <%= f.label :name %>
        <%= f.text_field :name, class: "input-lg" %>
        ...

        <%= f.fields_for :applied_programs do |fml| %>
            <%= fml.label :approved, "Approved:    " %><%= fml.check_box :approved %>
            <%= fml.hidden_field :id %>
        <% end %>

        <%= f.submit "Save Changes", class: "btn btn-lg btn-primary" %>
    <% end %>

测试仪型号:

has_many :applied_programs, :dependent => :destroy
accepts_nested_attributes_for :applied_programs

AppliedProgram Model:

belongs_to :tester

控制器:

def update_tester
  @applied_programs = AppliedProgram.where(tester_id: @tester.id)
  @tester.update_attributes(tester_params)
  @applied_programs.each do |p|
    p.update_attributes(tester_params)
  end
end

def tester_params
    params.require(:tester).permit(:name, :email, :phone_number, :address1, 
                                                                 :city, :zip_code, :country, :password, :password_confirmation,
                                                                 :active, :approved, applied_programs_attributes: [ :approved, :id ])
end

日志:

Processing by AdminsController#update_tester as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"x", "tester"=>{"name"=>"Dave", "email"=>"nope@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "applied_programs_attributes"=>{"0"=>{"approved"=>"0", "id"=>"9"}, "1"=>{"approved"=>"1", "id"=>"745"}}}, "commit"=>"Save Changes", "id"=>"16"}
Tester Load (0.1ms)  SELECT  `testers`.* FROM `testers`  WHERE `testers`.`id` = 16 LIMIT 1
Tester Load (1.1ms)  SELECT  `testers`.* FROM `testers`  WHERE `testers`.`remember_token` = 'x' LIMIT 1
(6.8ms)  BEGIN
AppliedProgram Load (2.6ms)  SELECT `applied_programs`.* FROM `applied_programs`  WHERE `applied_programs`.`tester_id` = 16 AND `applied_programs`.`id` IN (9, 745)
Tester Exists (0.8ms)  SELECT  1 AS one FROM `testers`  WHERE (`testers`.`email` = 'nope@gmail.com' AND `testers`.`id` != 16) LIMIT 1
(0.1ms)  COMMIT
AppliedProgram Load (0.1ms)  SELECT `applied_programs`.* FROM `applied_programs`  WHERE `applied_programs`.`tester_id` = 16
(0.1ms)  BEGIN
(12.4ms)  ROLLBACK
Completed 500 Internal Server Error in 95ms

ActiveRecord::UnknownAttributeError (unknown attribute: name)

1 个答案:

答案 0 :(得分:1)

你的问题是applied_programs上需要做的事情的循环:

def update_tester
  @applied_programs = AppliedProgram.where(tester_id: @tester.id)
  @tester.update_attributes(tester_params)
end

def tester_params
    params.require(:tester).permit(:name, :email, :phone_number, :address1, 
                                                                 :city, :zip_code, :country, :password, :password_confirmation,
                                                                 :active, :approved, applied_programs_attributes: [ :approved, :id ])
end