0

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

mu is too short
  • 396,305
  • 64
  • 779
  • 743
thiebo
  • 859
  • 1
  • 9
  • 23
  • Can you edit your question and add the ode for your `Bibliography` class? maybe it's getting the idea from there? (I'm guessing). – Taryn East Dec 19 '16 at 22:09

1 Answers1

1

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.

chester
  • 183
  • 1
  • 13
  • Thanks Chester. I did want to use the 'has_and_belongs_to_many` association as each author can have many books and each book can have many authors. But then, I didn't see how to populate my html form allowing to add several authors. – thiebo Dec 20 '16 at 05:43
  • @thiebo, it looks like there is a gem called [cocoon](https://github.com/nathanvda/cocoon) that can be used to address that problem. See [this](http://stackoverflow.com/questions/8352977/rails-forms-for-has-many-through-association-with-additional-attributes) question. – chester Dec 20 '16 at 16:00