Skip to content


Rails fields_for “is not allowed as an instance variable name”

So I’ve been trying to figure this error out, it just cropped up:

`@my_object[such_and_such_attributes]' is not allowed as an instance variable name

As is usually the case with Rails, the answer is pretty obvious once you understand what’s going on. Here’s the relevant code:

<% fields_for 'my_object[such_and_suches_attributes]', @my_object.such_and_suches.first do |such_and_such_form| -%>
  <%= such_and_such_form.text_field :name %>
<% end -%>

So why was the above creating the error? It’s because @my_object.such_and_suches was empty, thus Rails would try and use “my_object[such_and_suches_attributes]” as the attribute name which naturally didn’t work. This is the shortcut that allows this to work:

<% fields_for :posts do |post_form| -%>
  <%= post_form.text_field :title %>
<% end -%>

Now in my situation, why the “such_and_suches” is empty is a whole other problem…

Posted in Rails, Ruby, Software Development, Tips and Tricks.


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.