Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't update action work for nested form fields in rails?

I have a job model that belongs_to a profile model. A profile has_many jobs. I have a nested model form that in which a user adds jobs. The form is successfully adding jobs, but editing/updating is not working. Instead, when I try to edit a job, it keeps the old version of the job, but adds the new version as well. It does not replace the old version with the new one. How do I fix this? I'm pretty sure it has something to do with the edit/update controllers.

Edit controller:

def edit
    @profile = current_user.profile
end

Update controller:

def update
    #if current_user.profile.jobs.any?

    @profile = current_user.profile.update_attributes(profile_params)

    if current_user.profile.invalid?
        @profile = current_user.profile
        render :edit, :status => :unprocessable_entity
    else
        redirect_to profile_path(current_user.profile_name)
    end
end

The thing is, the update controller is working for the non-nested information, it is just not working for the nested jobs. Here are the strong parameters:

def profile_params
      params.require(:profile).permit(:title,
           :category, :description, :state, :zip_code, :rate, 
           jobs_attributes: [:firm, :position, :category, :description,
           :begin, :end, :_destroy])        
end

And here is the profile model:

class Profile < ActiveRecord::Base
    belongs_to :user
    has_many :jobs
    accepts_nested_attributes_for :jobs , :reject_if => :all_blank, :allow_destroy => true
end

Also, here's my routes if that will help:

resources :profiles do
   resources :jobs
end

Thanks for your help in advance.

EDIT

Here are the params from the create action:

{"jobs_attributes"=>{"1383593195849"=>{"firm"=>"1st firm", "position"=>"1st position",
 "category"=>"1st category", "description"=>"1st description", "begin"=>"1999",
 "end"=>"1999", "_destroy"=>"false"}}}

And here are the params for the same job when updated:

{"jobs_attributes"=>{"0"=>{"firm"=>"1st firm", "position"=>"1st position",
 "category"=>"1st category", "description"=>"1st description", "begin"=>"1999",
 "end"=>"1999", "_destroy"=>"false"}, "1"=>{"firm"=>"1st firm", 
 "position"=>"1st position", "category"=>"1st category", 
 "description"=>"1st description", "begin"=>"1999", "end"=>"1999", "_destroy"=>"1"}}}

EDIT:

Here are my views. I don't think they are part of the problem though.

= simple_form_for @profile do |f|

  %h3 Jobs
  #jobs
    = f.simple_fields_for :jobs do |job|
      = render 'job_fields', :f => job
    .links
      = link_to_add_association 'add job', f, :jobs
  = f.submit

And here is the "job_fields" partial:

.nested-fields
  = f.input :firm
  = f.input :position
  = f.input :category
  = f.input :begin
  = f.input :end
  = f.input :description
  = f.input :_destroy, as: :boolean, inline_label: 'Delete box'
  = link_to_remove_association "remove task", f
like image 307
Philip7899 Avatar asked Dec 26 '22 18:12

Philip7899


1 Answers

The trick is adding the ':id' symbol to the strong params. Although I still haven't figured out why and I'm not sure if its secure.

like image 184
Philip7899 Avatar answered Jan 23 '23 13:01

Philip7899