嵌套属性不允许我访问关联的模型数据

时间:2013-01-22 20:09:12

标签: ruby-on-rails form-for nested-form-for

我正在寻找为以下数据结构构建表单的正确方法:

class Profile < ActiveRecord::Base
  attr_accessible :name
  has_many :weights
  accepts_nested_attributes_for :weights
end

class Tag < ActiveRecord::Base
  attr_accessible :name
  has_many :weights
end

class Weight < ActiveRecord::Base
  attr_accessible :weight, :profile_id, :tag_id
  belongs_to :profile
  belongs_to :tag
end

在编辑个人资料表单中,我想提取所有权重并允许用户更新它们。我已经能够使用嵌套属性这样做:

<%= form_for [:admin, @profile] do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label :name %>
    <%= f.text_field :name %>
  </p>
  <div class='weights'>
    <%= f.fields_for :weights do |ff| %>
      <%= ff.label :weight %>
      <%= ff.text_field :weight %>
    <% end %> 
  </div>

  <%= f.submit %> 
<% end %>

问题是我实际上想要在每个权重行上引入相关tag_id的标题(这样人们就知道它们正在改变哪个权重标记)。我没有看到一种方法来提取这些信息,我应该在写这个表格之前做某种加入吗?这是一种愚蠢的做法吗?

谢谢大家

-Neil

1 个答案:

答案 0 :(得分:0)

您应该能够通过ff.object获得权重并通过ff.object.tag.title进行标记。你试过这个吗?

相关问题