Getting undefined method `values_at' for nil:NilClass using nested_form_for

江枫思渺然 提交于 2019-12-06 01:58:39

I was stuck in the same "undefined method" error and everything worked fine after I changed the first parameter of the f.fields_for call to the plural form, just like the has_many association. So in your case it should be:

= f.fields_for :invoice_line_items do |line_item|

since the association in the Invoice model is has_many :invoice_line_items

Hope it helps someone.

It looks like you have two separate problems.

1) The remove link doesn't do anything. What version of Rails are you using? The asset pipeline wasn't added until 3.1, so if you are using something lower you can just follow the instructions on Github under 'Non Asset Pipeline Setup' which might solve issue one.

2) Using the link_to_add helper gives you an error. I peeked at the code here https://github.com/ryanb/nested_form/blob/master/lib/nested_form/builder_mixin.rb

The method calls values_at on

@fields[fields_blueprint_id]

which, according to your error message, is apperently nil. I got a lost looking at Ryan's code. I don't see how this value gets set, so I can't do much to help you figure out why this value is nil. But if I had to guess, it's because you didn't add

attr_accessible :invoice_line_items_attributes

to your Invoice model

Here's what I came up with. If there were no items on the nested side of the relationship - say, if the book had no authors - I got the error. When I loaded the @book in the controller, I tried checking to see if the authors array was empty, and adding a new, empty Author if it was. When I did that, the nested form never saw an empty relation, and the errors went away.

I suspect this is a gotcha that could be coded out of nested_form if anyone wanted to do it and submit the pull request.

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