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が更新されます。

私がやりたいのは、ユーザーが値を入力して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'

投稿すると

4

1 に答える 1

1

ここで例を試してください

http://github.com/ryanb/complex-form-examples

于 2010-04-15T01:36:56.290 に答える