我遇到了让我头痛的问题。
在我的终端中我看到了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但没有使用in。
的表单...
</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的关联不需要。
如果我在trail表中使用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:
有谁知道为什么它会搜索冒险表而不是冒险表的试用表?
有人能指出我做错了吗?或指出我正确的方向来解决这个问题?
答案 0 :(得分: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