“accepts_nested_attributes_for”不渲染 _form.haml 部分中的字段

发布于 2024-12-12 05:19:38 字数 2761 浏览 1 评论 0原文

我有一个问题让我很头疼。

在我的终端中,我看到 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”指南中建议的设置相同。我仍然无法让表单显示在浏览器中进行编辑。

.../views/adventures/_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

我的 Trails 表中确实有一个 Adventure_id,但我已恢复为现在尝试更简单的方法,因为据说嵌套属性的关联不需要它。

如果我使用 Trails 表中的 Adventure_id 运行它,就像它尝试从 Adventure 表中选择 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: 

有谁知道为什么它会在 Adventures 表而不是 Trials 表中搜索 Adventure_id ?

谁能指出我做错了什么?或者指出正确的方向来解决这个问题?

I've got a problem that is giving me a headache.

in my Terminal I see that the Trail table is looked up. There is some dummy stuff in that table.

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

I've tried and erred on this for way to long, now this is using the same set up as Padrino's guide suggests in their "accepts_nested_attributes_for" guide. Still I can't get the form to show up in my browser for editing.

.../views/adventures/_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"

In the html source there is rendered the group_trail but none of the forms with in.

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

in my models I have:

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

I did have a adventure_id in my trails table, but I've reverted to simpler method of trying for now, since it's said not to be needed for this association of nested_attributes.

If I run it with the adventure_id in trails table there is like it tries to select the adventure_id from the adventure table.

     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: 

Does anyone know why it searches the adventures table instead of trials table for the adventure_id?

Can anyone point at what I'm doing wrong? or point me in the right direction to figure this out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可是我不能没有你 2024-12-19 05:19:38

也许问题出在自我引用上。

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

Maybe the problem is with the self reference.

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

What is the:

foreign_key ?

I.e:

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

Or if you have it in another table you should:

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

I think you can got the error also from your console:

Adventure.first.trails
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文