0

モデル:

product.rb:

class Product < ActiveRecord::Base
  attr_accessible :cat_id, :id, :name
  belongs_to :category
end

カテゴリ.rb:

class Category < ActiveRecord::Base
  attr_accessible :id, :name
  has_many :products
end

ルート:

ルート.rb

resources :categories do
  resources :products
end

商品をカテゴリに追加したい。コントローラーと製品のビューに何を書くか助けてください! お願いします!!Rails初心者です!

4

2 に答える 2

0

ドロップダウン メニューが必要な場合:

_file.html.haml

  = label_tag :Category
   = select_tag "product[category_id]", options_from_collection_for_select(@categories, :id,:name,@selected = @product[:category_id] :include_blank => true

products_controller.rb:

before_filter :get_categories, :only => [:new,:edit]


def get_categories
   @categories = Category.all
end
于 2013-07-25T10:05:45.937 に答える