0

関連するモデルは次のとおりです。

class ListItem < ActiveRecord::Base
    belongs_to  :inventory_item
    belongs_to  :shopping_list
    belongs_to  :item
end

class ShoppingList < ActiveRecord::Base
    has_many :list_items
    belongs_to  :user, :foreign_key => :user_id
end

class InventoryItem < ActiveRecord::Base

    belongs_to  :item, :foreign_key => :item_id
    belongs_to  :vendor
    has_many    :list_items
end

ListItems自分が所有するユーザー指定のリストに属する作成するボタンが必要です。newもそれぞれのおよびListItemに渡す必要があります。これが私の現在のビューの関連部分です。:item_id:inventory_item_id

<tr>
        <% item.inventory_items.each do |product| %>
        <td><%= button_to "#{product.price}",
                           {:controller => :list_items,
                            :action => 'create',
                            :id => #what goes here??,
                            :method => :create %></td>
        <% end %>
</tr>

そして、私の ListItems コントローラー create メソッド:

def create
        ListItem.create
        flash[:success] = "List Item Added."
        redirect_to search_results_path(params[:search])
end

明らかに、私の create メソッドは、 以外の属性を持たない ListItem を作成するだけなので、現時点ではそれほど役に立ちません:id。適切なパラメーターをコントローラーに渡す最良の方法は何ですか? どんな助けでも大歓迎です!前もって感謝します。

4

1 に答える 1