2011-10-26 34 views
1

我遇到了一個令我頭疼的問題。「acceptable_nested_attributes_for」不會在_form.haml中呈現字段部分

在我的終端中我看到Trail表被擡頭。該表中有一些虛擬的東西。

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」指南建議。儘管如此,我仍然無法將表單顯示在瀏覽器中進行編輯。

... /視圖/冒險/ _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" 

在HTML源代碼有被渲染的形式group_trail但沒有用英寸

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

在我的模型有:

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,但我已經恢復到更簡單的方法嘗試現在,因爲據說這種nested_attributes的關聯不需要。

如果我在小徑表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冒險表而不是trial_table冒險表?

任何人都可以指出我做錯了什麼?或者指出我正確的方向來解決這個問題?

回答

1

也許問題是與自我參考。

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

什麼:

foreign_key ? 

即:

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

或者,如果你在另一個表,你應該有它:

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

我認爲你可以得到該錯誤也來自您的控制檯:

Adventure.first.trails 
+0

感謝您指出的外鍵...這是我忘了定義。它仍然沒有出現在我的渲染_form中。但我已經採取了另一種方式,最終似乎更好。 你確實是Padrino的Padrino。 – Halakarta

+0

嗯..它的工作!在我看來/ _form.haml 再次感謝。 我希望我能夠詳細記錄我所有的記錄,但是我不記得我所做的與這個問題直接相關的所有事情。 – Halakarta