2011-10-26 11 views
1

私に頭痛を与えている問題があります。"accepts_nested_attributes_for"は_form.hamlのフィールドを部分的にレンダリングしません

私の端末では、トレイルテーブルが参照されていることがわかります。そのテーブルにはいくつかのダミーのものがあります。

DEBUG - Account Load (0.4ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."id" = 1 LIMIT 1 
DEBUG - Trail Load (0.2ms) SELECT "trails".* FROM "trails" 
DEBUG - TEMPLATE (0.0004ms) /adventures/new 
DEBUG - TEMPLATE (0.0002ms) /layouts/application 
DEBUG - TEMPLATE (0.0004ms) /adventures/_form 
DEBUG - TEMPLATE (0.0003ms) /base/_sidebar 
DEBUG -  GET (0.0644ms) /admin/adventures/new - 200 OK 

私が試したし、長い間への道のために、この上の誤りを犯し、今これはPadrinoのガイド彼らの「accepts_nested_attributes_for」ガイドに示唆するのと同じセットを使用していました。それでも私のフォームをブラウザに表示して編集することはできません。

中でフォームのgroup_trailが、どれもがレンダリングされるHTMLソースで

... /ビュー/冒険/ _form.haml

.group_trail 
    -f.fields_for :trails do |trail_form| 
    =trail_form.label :start 
    =trail_form.text_field :start 
=trail_form.label :end 
    =trail_form.text_field :end 
    =trail_form.label :via 
    =trail_form.text_field :via 
    -unless trail_form.object.new_record? 
     =trail_form.check_box '_destroy' 
     =trail_form.label '_destroy', :caption => "Remove" 

...  
</div> 
<div class='group_trail'> 
    </div> 
    <div class='group navform wat-cf'> 
    <input class="button" value="Save" type="submit" /> 

私のモデルではI持っている:

class Adventure < ActiveRecord::Base 
    has_many :trails, :class_name => 'Adventure' 
    accepts_nested_attributes_for :trails, :allow_destroy => true 
end 

class Trail < ActiveRecord::Base 
    belongs_to :adventure 
end 

私のトレイルテーブルにはadventure_idがありましたが、試してみると簡単な方法に戻りましたこれは、このネストされた属性の関連付けには必要ないと言われているからです。

trailsテーブルのadventure_idで実行すると、冒険テーブルからadventure_idを選択しようとしたようです。

 DEBUG - Account Load (0.3ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."id" = 1 LIMIT 1 
     DEBUG - Trail Load (0.2ms) SELECT "trails".* FROM "trails" 
     DEBUG - Adventure Load (0.2ms) SELECT "adventures".* FROM "adventures" WHERE "adventures"."id" = ? LIMIT 1 [["id", "9"]] 
     DEBUG - TEMPLATE (0.0004ms) /adventures/edit 
     DEBUG - TEMPLATE (0.0002ms) /layouts/application 
     DEBUG - TEMPLATE (0.0003ms) /adventures/_form 
     DEBUG - Adventure Load (0.3ms) SELECT "adventures".* FROM "adventures" WHERE "adventures"."adventure_id" = 9 
     DEBUG - SQLite3::SQLException: no such column: adventures.adventure_id: SELECT "adventures".* FROM "adventures" WHERE "adventures"."adventure_id" = 9 
    ActiveRecord::StatementInvalid - SQLite3::SQLException: no such column: adventures.adventure_id: SELECT "adventures".* FROM "adventures" WHERE "adventures"."adventure_id" = 9: 

adventure_idの試用表ではなく、アドベンチャーテーブルを検索する理由は誰にも分かりますか?

誰かが間違っていることを指摘できますか?これを理解するために正しい方向に私を向ける?

答えて

1

おそらく、問題は自己参照である可能性があります。

has_many :trails, :class_name => 'Adventure', :foreign_key => :trail_id 

それとも、あなたがすべき別のテーブルでそれを持っている場合:

foreign_key ? 

すなわち:

class Adventure < ActiveRecord::Base 
    has_many :trails, :class_name => 'Adventure' 
    accepts_nested_attributes_for :trails, :allow_destroy => true 
end 

は何ですか

has_many :trails, :class_name => 'Adventure', :foreign_key => :trail_id, :table_name => :trails 

私はあなたが得たことができると思いますあなたのコンソールからのエラー:

Adventure.first.trails 
+0

外部キーを指摘してくれてありがとう...それは私が定義するのを忘れていたものでした。まだレンダリングされた_formには表示されません。しかし、私はそれの周りに別の方法を行った、実際には終わりの方が良いようです。 あなたは真にパドリーノのパドリーノです。 – Halakarta

+0

さて、それは動作します!私の見解で/ _form.haml もう一度ありがとう。 私はすべてのステップを詳細にバックトレースすることができますが、私はこの問題に直接関連するすべてを覚えていません。 – Halakarta

関連する問題