Reputation: 2011
I have a model ProductFeature
a controller called product_feature_controller.rb
I am writing the 'edit' method of my CRUD for this controller. In the edit method, I created an instance variable named @productFeature
and set it equal to ProductFeature.find(:id)
and I send this instance variable to my view to be used by a form_for(:product_feature)
. I have found that my form will not populate its fields with the current data from the record I am pulling up. After pulling hairs for some hours, it dawned on me that my error was all in the way named my instance variable.
It should be named @product_feature instead of @productFeature. This seems like a trivial detail, but I'd really like to know why this is the case. How does this even work? I mean how does Rails know to populate my form based on the way the instance Variable is named? I hope my question is clear, just seeking further edification.
Upvotes: 0
Views: 494
Reputation: 160170
Because the symbol :product_feature
(in the form_for
) is going to look up an instance variable based on that symbol. It doesn't do any transformation, and will be looking for a @product_feature
variable. As long as they match, you should be okay, but the Ruby world uses underscores between words.
Also, models should be named with a leading capital, ProductFeature
, by convention. I don't know if this will cause any issues, but it's counter to the norm.
Upvotes: 1