使用accepts_nested_attributes_for 和来自另一个表的预填充的嵌套表单

发布于 2024-08-28 20:07:31 字数 2297 浏览 4 评论 0原文

我正在使用 Rails 2.3.5 并且有一个嵌套结构,如下所示:

Lists has_many Items

Items_Features has_many Features
Items_Features has_many Items

Items_Features has a text field to hold the value of the feature

然后我有一个带有部分的嵌套表单来更新和显示它,以便它更新列表,项目和 Items_Features

我想要做的是为每个生成输入字段features 中的行,以便用户可以填写值并在 items_features 中插入/更新。我还想要在框旁边有一个标签来显示功能名称。

它可能看起来像这样:

List name: Cheeses
    Item1 name: Edam
         Feature, hardness: - fill in - <= this list of features from feature table
         Feature, smell: - fill in -

我怎样才能中断漂亮且简单的accepts_nested_attributes_for系统来按我想要的方式显示它?

编辑:

这是 Item 类的代码,现在我在那里有一些 sql:

class Item < ActiveRecord::Base
  belongs_to :list
  has_many :users
  has_many :votes
  has_many :items_features
  validates_presence_of :name, :message => "can't be blank"
  accepts_nested_attributes_for :items_features, :reject_if => lambda { |a| a.values.all?(&:blank?) }, :allow_destroy => true

  def self.get_two_random(list_id)
    Item.find_by_list_id(list_id, :limit => 2, :order => 'rand()')
  end

  def self.get_item_features(item_id)
    sql =  "select items_features.*, features.id as new_feature_id, features.name as feature_name "
    sql += "from items_features "
    sql += " right outer join features "
    sql += "  on features.id = items_features.feature_id "
    sql += " left outer join items "
    sql += "  on items.id = items_features.item_id "
    sql += "where items_features.item_id = ? or items_features.item_id is null"

    find_by_sql([sql, item_id])
  end

end

这是我的显示代码 - 我需要以某种方式将 feature_id 保存到新记录中:

<% form_tag item_path, :method => :put do %>
 <% for items_feature in @item_features %>
  <% fields_for "items_features[]", items_feature do |f| %>
   <%=h items_feature.feature_name %> <%= f.text_field :featurevalue %><br><Br>
  <% end %>
 <% end %>
 <p><%= submit_tag "Submit"%></p>
<% end %>

嗯,这不起作用 - 它打印出来很好,但是然后服务器给我:

/!\ FAILSAFE /!\  Thu Apr 15 16:44:59 +0100 2010
Status: 500 Internal Server Error
expected Array (got Hash) for param `items_features'

当我发回时

I'm using Rails 2.3.5 and have a nested structure as follows:

Lists has_many Items

Items_Features has_many Features
Items_Features has_many Items

Items_Features has a text field to hold the value of the feature

Then I have a nested form with partials to update and display this so that it updates Lists, Items and Items_Features

What I want to do is generate input fields for each of the rows in features so that the user can fill in a value and it gets inserted/updated in items_features. I also want a label next to the box to display the feature name.

It might look like this:

List name: Cheeses
    Item1 name: Edam
         Feature, hardness: - fill in - <= this list of features from feature table
         Feature, smell: - fill in -

How can I interrupt the nice and easy accepts_nested_attributes_for system to display this as I want?

EDIT:

Here's the code for the Item class now i've got some sql in there:

class Item < ActiveRecord::Base
  belongs_to :list
  has_many :users
  has_many :votes
  has_many :items_features
  validates_presence_of :name, :message => "can't be blank"
  accepts_nested_attributes_for :items_features, :reject_if => lambda { |a| a.values.all?(&:blank?) }, :allow_destroy => true

  def self.get_two_random(list_id)
    Item.find_by_list_id(list_id, :limit => 2, :order => 'rand()')
  end

  def self.get_item_features(item_id)
    sql =  "select items_features.*, features.id as new_feature_id, features.name as feature_name "
    sql += "from items_features "
    sql += " right outer join features "
    sql += "  on features.id = items_features.feature_id "
    sql += " left outer join items "
    sql += "  on items.id = items_features.item_id "
    sql += "where items_features.item_id = ? or items_features.item_id is null"

    find_by_sql([sql, item_id])
  end

end

Here's my display code - I need to save the feature_id into new records somehow:

<% form_tag item_path, :method => :put do %>
 <% for items_feature in @item_features %>
  <% fields_for "items_features[]", items_feature do |f| %>
   <%=h items_feature.feature_name %> <%= f.text_field :featurevalue %><br><Br>
  <% end %>
 <% end %>
 <p><%= submit_tag "Submit"%></p>
<% end %>

Hmm, that isn't working - it prints out fine but then the server gives me:

/!\ FAILSAFE /!\  Thu Apr 15 16:44:59 +0100 2010
Status: 500 Internal Server Error
expected Array (got Hash) for param `items_features'

When I post it back

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

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

发布评论

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

评论(1

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