我正在制作一个基于railscast教程346(Wicked gem)和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
强大的param方法:
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
如您所见:&#34;未经许可的参数:educations_attributes&#34;。属性school_name,degree,date_started和date_finished未保存到数据库中。我已经做过研究如何解决这个问题,但我没有成功。我现在不知道该怎么做。
非常感谢任何回应或帮助。我是rails btw的新手。感谢。
答案 0 :(得分:2)
当您使用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