John Powel
John Powel

Reputation: 1424

Rails: How to access child model params in controller?

I have two models submission and upload where:

#Submission.rb
attr_accessible :id, :photo_id, :uploads_attributes
has_many :uploads, :dependent => :destroy
accepts_nested_attributes_for :uploads, :allow_destroy => true

#Upload.rb:
attr_accessible :id,:photo_type_id    
belongs_to :submission

In my submissions_controller.rb I can access photo_id with:

params[:submission][:photo_id],

however I don't know how to access its child model's photo_type_id

I tried

params[:uploads_attributes][0][:package_type_id]

but it doesnt work, ig gives me:

NoMethodError (undefined method `[]' for nil:NilClass):

In my view page source I have:

name="submission[photo_id]"

**in fields_for:**

name="submission[uploads_attributes][0][photo_type_id]"

Any idea what should be changed?

Upvotes: 0

Views: 432

Answers (1)

John Powel
John Powel

Reputation: 1424

Find the answer here:

How to access nested params

in my case, I used:

params[:submission][:uploads_attributes]["0"][:photo_type_id]

Just need to change the [0] to ["0"]

Upvotes: 2

Related Questions