nested model attributes + wicked wizard form not working

你说的曾经没有我的故事 提交于 2019-12-23 02:17:26

问题


I am making an app based on the railscast tutorial 346(Wicked gem) and 196(nested model). I am trying to combine these 2 together and I am stuck because strong param attributes are not saving.

So my app goes like this: I have a user, work, and education 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

And then i generated the wicked wizard controller which i called 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

strong param method:

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

My education and work steps view look like this:

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

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.


回答1:


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


来源:https://stackoverflow.com/questions/28749770/nested-model-attributes-wicked-wizard-form-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!