couch334
couch334

Reputation: 45

No implicit conversion of Symbol into Integer, nested forms

While creating new Student Im getting error "no implicit conversion of Symbol into Integer"

In Students Controller,

def student_params
  params.require(:student).permit(:name, :lastname, :subjects_attributes [:id, :name :_destroy, :mark_attributes [ :id, :value ]] )
end

What causes this problem ?

Upvotes: 2

Views: 3723

Answers (2)

jvillian
jvillian

Reputation: 20263

Bilal is correct. Also, you'll have to change :mark_attributes to mark_attributes:.

Why?

:subjects_attributes is a symbol. But subjects_attributes: [ ] is a hash where the key is :subjects_attributes (a symbol, as it turns out) and the value is [ ].

So, strong parameters knows how to process the hash defined by subjects_attributes: [ ] just fine.

But a symbol followed by an array, like :subjects_attributes [ ]? Well, that makes for all kinds of unhappiness accompanied by falling on the floor, kicking, and screaming.

As Bilal also points out, you can get back to a place of happiness by doing :subjects_attributes => [ ], which also creates hash and makes the sun shine again.

And that, my friend, is the answer to the question "What causes this problem?"

Upvotes: 2

Arslan Ali
Arslan Ali

Reputation: 17802

The problem is here:

:subjects_attributes [:id, :name :_destroy, :mark_attributes [ :id, :value ]] )

You should have a colon(:) after subject_attributes, not before it.

You can do either :subject_attributes => [:id, :name, :_destroy...] or subject_attributes: [:id, :name, :_destroy...]

The syntax without => is used with Ruby 2.0+, and is preferred one.

Upvotes: 5

Related Questions