助手“fields_for”不工作

发布于 2024-11-09 14:13:39 字数 1218 浏览 5 评论 0原文

我正在使用嵌套属性,但这些字段未加载到我的视图中。 有人知道我错过了什么吗?

Rails 3.1、Ruby 1.9.2

模型 1:

class Traditions::Material < ActiveRecord::Base
  has_one :material_asset, :dependent => :destroy
  validates_presence_of :title
  accepts_nested_attributes_for :material_asset
end

模型 2:

class Traditions::MaterialAsset < ActiveRecord::Base
  belongs_to :material
  has_attached_file :asset
  validates_attachment_presence :asset
end

视图 (HAML):

= form_for @material, :html => {:class => 'form', :multipart => true} do |f|
    = errors_for @material

    .field
        = f.label :title
        = f.text_field :title

    .field
        = f.label :description
        = f.text_area :description, :rows => 5

    .field
        = f.fields_for :material_asset do |ma|
            = ma.label :asset
            = ma.file_field :asset

    .buttonrow
        = f.submit 'Save'

结果 HTML(部分):

<div class='field'></div>
<div class='buttonrow'>
  <input name="commit" type="submit" value="Save" />
</div>

在上面,div.field 为空。

I'm using nested attributes, but the fields aren't loaded in my view.
Someone know what I missing?

Rails 3.1, Ruby 1.9.2

Model 1:

class Traditions::Material < ActiveRecord::Base
  has_one :material_asset, :dependent => :destroy
  validates_presence_of :title
  accepts_nested_attributes_for :material_asset
end

Model 2:

class Traditions::MaterialAsset < ActiveRecord::Base
  belongs_to :material
  has_attached_file :asset
  validates_attachment_presence :asset
end

View (HAML):

= form_for @material, :html => {:class => 'form', :multipart => true} do |f|
    = errors_for @material

    .field
        = f.label :title
        = f.text_field :title

    .field
        = f.label :description
        = f.text_area :description, :rows => 5

    .field
        = f.fields_for :material_asset do |ma|
            = ma.label :asset
            = ma.file_field :asset

    .buttonrow
        = f.submit 'Save'

Result HTML (part):

<div class='field'></div>
<div class='buttonrow'>
  <input name="commit" type="submit" value="Save" />
</div>

In above, div.field is empty.

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

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

发布评论

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

评论(3

晨曦慕雪 2024-11-16 14:13:39

我从您的rails问题中偶然发现了这一点,它似乎没有在中构建嵌套资源您的操作。

您是否在控制器中调用 build_material_asset ?这可以解释为什么字段没有被渲染。如果嵌套对象为nil,则没有要为其渲染字段的对象。

尝试这样的事情:

class MaterialsController < ApplicationController
  def new
    @material = Traditions::Material.new
    @material.build_material_asset
  end
end

I stumbled across this from your rails issue, which appears to be not building the nested resource in your new action.

Are you calling build_material_asset in your controller? That could explain why the fields aren't getting rendered. If the nested object is nil, there are no object to render fields for.

Try something like this:

class MaterialsController < ApplicationController
  def new
    @material = Traditions::Material.new
    @material.build_material_asset
  end
end
洒一地阳光 2024-11-16 14:13:39

fields_for 有新语法。

= fields_for :plan, @plan do |builder| ...

在控制器中(新操作):

@plan = Parent.plans.new

在此处阅读更多信息: http: //api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for

fields_for has new syntax.

= fields_for :plan, @plan do |builder| ...

in controller (action new) :

@plan = Parent.plans.new

read more here: http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for

眉目亦如画i 2024-11-16 14:13:39

我无法检查,但类似这样的事情?

- form_for @material, :html => {:class => 'form', :multipart => true} do |f|
    = errors_for @material

    #field
        = f.label :title
        = f.text_field :title

    #field
        = f.label :description
        = f.text_area :description, :rows => 5

    #field
        - f.fields_for :material_asset do |ma|
            = ma.label :asset
            = ma.file_field :asset

    .buttonrow
        = f.submit 'Save'

问题是您嵌套了 =。您应该使用 - 进行外部迭代。另外,您确定 .field 有效吗?不是应该是#field吗?

I cannot check, but something like this?

- form_for @material, :html => {:class => 'form', :multipart => true} do |f|
    = errors_for @material

    #field
        = f.label :title
        = f.text_field :title

    #field
        = f.label :description
        = f.text_area :description, :rows => 5

    #field
        - f.fields_for :material_asset do |ma|
            = ma.label :asset
            = ma.file_field :asset

    .buttonrow
        = f.submit 'Save'

The problem is that you have nested =. You should use - for the outer iterations. Also, are you sure that .field works? Isn't it supposed to be #field?

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