Ruby on rails 使用的嵌套表单接受来自另一个表的预填充的嵌套属性
我使用的是Rails 2.3.5,其嵌套结构如下:Ruby on rails 使用的嵌套表单接受来自另一个表的预填充的嵌套属性,ruby-on-rails,nested-forms,Ruby On Rails,Nested Forms,我使用的是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 然后,我有一个嵌套表单,其中包含部分内容,以更新和显示该表单,从而更新列表、项目和项目功能 我想做的是为features中的每一行生成输入字段,
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
然后,我有一个嵌套表单,其中包含部分内容,以更新和显示该表单,从而更新列表、项目和项目功能
我想做的是为features中的每一行生成输入字段,以便用户可以填写一个值,并在features中插入/更新该值。我还希望框旁边有一个标签来显示要素名称
可能是这样的:
List name: Cheeses
Item1 name: Edam
Feature, hardness: - fill in - <= this list of features from feature table
Feature, smell: - fill in -
当我把它发回时,试试这里的例子
嗨,威尔,谢谢你看这个。事实上,我已经大量使用了这个例子,非常好。不过,这并不能让我达到目的。我现在正在尝试使用find_by_sql来获取包含“cheesefeatures”空值的记录,如上所述。我将用sql编辑上面的文章,这样你就可以明白我的意思了。它正在显示我想要的功能。我只需要用一个隐藏的字段来保存item_功能,该字段可能是功能id?
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
<% 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'