2016-12-19 16 views
0

私はレールを使って簡単なフォームを提出しようとしています。NameError、初期化されていない定数

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 

移行ファイルには同じフィールドが含まれています。 titreとsoustitreは両方とも文字列であり、auteur_unは整数です。

私がフォームを送信すると、私はエラー、取得:

NameError in BibliographiesController#creer 
uninitialized constant Bibliography::AuteurUn 

params_hashが含まれています

"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"=>""} 

をこれは私の参考文献クラスです:

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 

それレールがauteur_unがクラスまたは定数であると予想しているようです。しかし、私は理由を見ません。あなたが外国人のモデルを関連付けるとき

おかげ

+0

質問を編集し、あなたの 'Bibliography'クラスにodeを追加できますか?多分それはそこからアイデアを得ているでしょうか? (私は推測している)。チェスターさんに感謝します。 –

答えて

1

は一般的に、foreign_keyオプションは、例えば、has_one関連して使用する必要があります:some_modelsome_model_id以外の属性を使用して。

auteurs_idを使用して、4つの異なるhas_oneアソシエーションがあります。それはあなたのように私には見えます

Auteurモデルを持っている、とあなたはBibliographyモデルは、4つの関連付けを持つようにしたい:auteur_un:auteur_deux:auteur_trois:auteur_quatre、異なるAuteurインスタンスを指し、それぞれ。

それは確かにそうであるならば、私の代わりに、あなたがbelongs_to関連付けを使用したいと思う:

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 

この協会はauteur_un_idauteur_deux_idauteur_trois_id、およびauteur_quatre_idを持っているBibliographyモデルを期待しています、 "auteur"列を変更する必要があります(それぞれに "_id"を追加してください)。例えば、あなたのプライベートのparams方法がに変更になります。また

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 

、あなたは列名を変更せずにforeign_keyオプション、使用することができます

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 

をしかし、鉄道の規則に行くための最善の方法でしょう。

+0

それぞれの著者は多くの本を持ち、各本は多くの作家を持つことができるので、私は 'has_and_belongs_to_many'の関連付けを使いたいと思っていました。しかし、その後、私はいくつかの著者を追加することができるように私のhtmlフォームを埋める方法を見ていませんでした。 – thiebo

+0

@thiebo、その問題に対処するために使用できる[cocoon](https://github.com/nathanvda/cocoon)という宝石があるようです。 [this](http://stackoverflow.com/questions/8352977/rails-forms-for-has-many-through-association-with-additional-attributes)の質問を参照してください。 – chester

関連する問題