I got an error with my check_box helper and don’t understand what I am doing wrong :
undefined method `prive' for #<PubContributeur:0xa63b8fc>
This my app/models/pub_contributeur.rb :
class PubContributeur < ActiveRecord::Base
attr_accessible :nom, :prive
validates :nom, :presence => true
validates :nom, :length => { :in => 2..30 }, :uniqueness => { :case_sensitive => false }
end
I’m using a namespace catalogs on my controller.
This is my HAML like view app/views/catalogs/pub_contributeurs/_form.html.haml :
= form_for [:catalogs, @pub_contributeur], :html => {:class => "formulaire-standard"} do |f|
= render :partial => 'shared/error_message', :locals => { :element => @pub_contributeur, :debut_erreur => 'Ce contributeur ne peut être enregistré'}
.groupe-champs
.champ
= f.label :nom
= f.text_field :nom, :class => 'input-width-8-col', :required => 'required'
.champ
= f.label :prive
= f.check_box :prive
Any idea ?
I found the solution !
When I replace the
line in my _form.rb with a debug on my instance variable :
I can see something strange :
I did check my db/migration/ and found the issue.
My migration file was creating a string field called “description” instead of a boolean field called “prive“.
If we think a bit about the first error message. This is very weird because the class in the model file do have a “prive” accessible attribut that may mislead us to think the class do have “nom” and “prive” as attibuts.
To understand what is happening we must have an advanced knowledge about Rails and Ruby. This comes from the inheritance of ActiveRecord::Base.
The magic of Rails behind the scene creates attributes for the PubContributeur Class based on the name of the columns in the table pub_contributeurs of my DB.
I wish Rails error message could have been a bit more explicit ^^