Question: NameError, uninitialized constant

Question

NameError, uninitialized constant

Answers 1
Added at 2016-12-19 23:12
Tags
Question

I'm trying to submit a simple form using rails.

class BibliographiesController < ApplicationController
   def creer #this has been localized for create
       @bibliography = Bibliography.new(bibliographie_params)
       @bibliography.save
       redirect_to @bibliography
   end
   #etc etc

private

    def bibliographie_params
        params.require(:bibliographie).permit(:titre, :soustitre, :auteur_un, :auteur_deux, :auteur_trois, :auteur_quatre, :genre, :recueil, :review, :revue_numero, :annee, :revue_page, :editeur, :lieu, :commentaire)
    end
end

The migration file contains the same fields. titre and soustitre are both strings and auteur_un is an integer.

When I submit the form, I get an error, which is :

NameError in BibliographiesController#creer
uninitialized constant Bibliography::AuteurUn

the params_hash contains :

"bibliographie"=>{
"titre"=>"La vie urbaine à Douai au Moyen-Âge", 
"soustitre"=>"rien", 
"auteur_un"=>"1", 
"genre"=>"source", 
"recueil"=>"aucun", 
"review"=>"", 
"revue_numero"=>"", 
"annee"=>"", 
"revue_page"=>"", 
"editeur"=>"", 
"lieu"=>"", 
"commentaire"=>""}

This is my Bibliography class :

class Bibliography < ApplicationRecord
    has_one :auteur_un, foreign_key: "auteurs_id"
    has_one :auteur_deux, foreign_key: "auteurs_id"
    has_one :auteur_trois, foreign_key: "auteurs_id"
    has_one :aauteur_quatre, foreign_key: "auteurs_id"
    has_one :review, foreign_key: "reviews_id"
end

It seems like rails is expecting auteur_un to be a class or constant. But I don't see why.

Thanks

Answers
nr: #1 dodano: 2016-12-20 01:12

Generally, the foreign_key option should be used with the has_one association when you want to associate a foreign model, eg: some_model, using an attribute other than some_model_id.

You have four different has_one associations using auteurs_id.

It looks to me like you have an Auteur model, and you want the Bibliography model to have four associations :auteur_un, :auteur_deux, :auteur_trois, :auteur_quatre, each pointing to a different Auteur instance.

If that is indeed the case, I think you want to use the belongs_to association, instead:

class Bibliography < ApplicationRecord
    belongs_to :auteur_un, class_name: "Auteur"
    belongs_to :auteur_deux, class_name: "Auteur"
    belongs_to :auteur_trois, class_name: "Auteur"
    belongs_to :auteur_quatre, class_name: "Auteur"
    ...
end

This association will expect the Bibliography model to have an auteur_un_id, auteur_deux_id, auteur_trois_id, and an auteur_quatre_id, so you will need to modify your "auteur" columns (append "_id" to each). Eg, your private params method would change to:

def bibliographie_params
    params.require(:bibliographie).permit(:titre, :soustitre, :auteur_un_id, :auteur_deux_id, :auteur_trois_id, :auteur_quatre_id, :genre, :recueil, :review, :revue_numero, :annee, :revue_page, :editeur, :lieu, :commentaire)
end

Alternatively, you could use the foreign_key option, without changing the column names:

class Bibliography < ApplicationRecord
    belongs_to :auteur_un, class_name: "Auteur", foreign_key: "auteur_un"
    belongs_to :auteur_deux, class_name: "Auteur", foreign_key: "auteur_deux"
    belongs_to :auteur_trois, class_name: "Auteur", foreign_key: "auteur_trois"
    belongs_to :auteur_quatre, class_name: "Auteur", foreign_key: "auteur_quatre"
    ...
end

But it would be best practice to go with Rail's conventions.

Source Show
◀ Wstecz