嵌套模型属性 + 邪恶的向导形式不起作用

nested model attributes + wicked wizard form not working

我正在根据 railscast 教程 346(Wicked gem) 和 196(nested model) 制作一个应用程序。我试图将这两个组合在一起,但我被卡住了,因为强参数属性没有保存。

所以我的应用程序是这样的:我有一个用户、工作和教育模型。

user.rb:

has_many :educations, dependent: :destroy
has_many :works, dependent: :destroy
accepts_nested_attributes_for :educations, :works

education.rb

belongs_to :user

work.rb

belongs_to :user

然后我生成了我称之为 UserStepsController 的邪恶向导控制器:

include Wicked::Wizard

steps :education, :work

def show
    @user = current_user
    @education = @user.educations.build
    @work = @user.works.build
    render_wizard
end

def update
    @user = current_user
    case step
    when :education
        @education = @user.educations.build
        @education.update_attributes(user_params)
        render_wizard @education
    when :work
        @work = @user.works.build
        @work.update_attributes(user_params)
        render_wizard @work
    end
end

强参数方法:

def user_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :county_id, :area_id, :client, educations: [:school_name, :degree, :year_started, :year_finished], works: [:company_name, :work_title, :date_started, :date_finished])
end

我的教育和工作步骤视图如下所示:

<h1>Education</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
    <%= simple_form_for @user, url:wizard_path do |f| %>
    <%= f.simple_fields_for :educations do |b| %>
    <%= b.input :school_name %>
    <%= b.input :degree %>
    <%= b.input :year_started %>
    <%= b.input :year_finished %>
    <% end %>
    <%= f.button :submit, "Continue" %>
    <% end %>
</div>

<h1>Work</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
    <%= simple_form_for @user, url:wizard_path do |f| %>
    <%= f.simple_fields_for :works do |b| %>
    <%= b.input :company_name %>
    <%= b.input :work_title %>
    <%= b.input :date_started %>
    <%= b.input :date_finished %>
    <% end %>
    <%= f.button :submit, "Continue" %>
    <% end %>
</div>

现在,当我在教育或工作步骤页面中单击继续后,我得到了这个:

Started PATCH "/user_steps/education" for ::1 at 2015-02-26 18:03:27 +0000
Processing by UserStepsController#update as HTML
  Parameters: {"utf8"=>"V", "authenticity_token"=>"/UxsLRNrLDxEeVHLaFmRjySveLty2
UXM+x8sA058o3gdLicRGCHRSV+qBkbJtHFNtX5WXJVhEQOKFMGdKyTLhg==", "user"=>{"educatio
ns_attributes"=>{"0"=>{"school_name"=>"ssddaddsa", "degree"=>"sddssd", "year_sta
rted(1i)"=>"2015", "year_started(2i)"=>"2", "year_started(3i)"=>"26", "year_fini
shed(1i)"=>"2015", "year_finished(2i)"=>"2", "year_finished(3i)"=>"26"}}}, "comm
it"=>"Continue", "id"=>"education"}
  User Load (1.0ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 140 LIM
IT 1
Unpermitted parameter: educations_attributes
   (0.0ms)  BEGIN
  SQL (1.0ms)  INSERT INTO `educations` (`user_id`, `created_at`, `updated_at`)
VALUES (140, '2015-02-26 18:03:27.130368', '2015-02-26 18:03:27.130368')
   (5.0ms)  COMMIT
   (0.0ms)  BEGIN
   (1.0ms)  COMMIT

如您所见:"Unpermitted parameter: educations_attributes"。属性 school_name、度数、date_started 和 date_finished 未保存到数据库中。我已经研究过如何解决这个问题,但我没有成功。我现在不知道怎么办。

任何回复或帮助将不胜感激。我是 rails 顺便说一句的新手。谢谢。

当您使用 accepts_nested_attributes_for 时,您必须指定您的属性以将它们列入白名单:educations_attributes 和 works_attributes(如 API 所示)

def user_params
      params.require(:user).permit(
        :first_name, :last_name, :email, :password,
        :password_confirmation, :county_id, :area_id, :client, 
        educations_attributes: [:school_name, :degree, :year_started, :year_finished], 
        works_attributes: [:company_name, :work_title, :date_started, :date_finished]
      )
    end