嵌套的模型属性+邪恶的向导形式不起作用 [英] nested model attributes + wicked wizard form not working

查看:98
本文介绍了嵌套的模型属性+邪恶的向导形式不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于Railscast教程346(邪恶的宝石)和196(嵌套模型)制作一个应用程序.我试图将这两个结合在一起,但由于无法保存强大的param属性而卡住了.

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

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

您将看到:不允许的参数:educations_attributes".属性school_name,学位,date_started和date_finished不会保存到数据库中.我已经进行了研究,如何解决此问题,但没有成功.我不知道现在该怎么办.

任何答复或帮助将不胜感激.我是Rails Btw的新手.谢谢.

解决方案

在使用accepts_nested_attributes_for时,必须指定属性以将其列入白名单:educations_attributes和works_attributes(如

<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>

Now, when Im in education or work step page, after clicking continue, I am getting this:

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

As you can see: "Unpermitted parameter: educations_attributes". The attributes school_name, degree, date_started, and date_finished are not saving into database. I already did a research how can I fix this but I am not succeeding. I don't know what to do now.

Any reponse or help will be very much appreciated. I am newbie in rails btw. Thanks.

解决方案

When you're using accepts_nested_attributes_for you must specify your attributes to whitelist them: educations_attributes and works_attributes (as shown in the 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

这篇关于嵌套的模型属性+邪恶的向导形式不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆