【发布时间】:2012-02-11 00:01:47
【问题描述】:
我有两个模型,Story 和 Category。使用Story#new 的表单,我希望能够保存类别的外键。
要在我使用过的Story#new 表单页面上显示类别中的选择数据:
<%= collection_select(:category , :category, Category.all , :id, :category, {:prompt => 'Select Category...'}) %>
如何将category_id 保存到新创建的 Story 对象中?
故事有属性:industry_id 和user_id,类别有属性name
development.log 告诉我:(看起来它正在尝试将新类别 (99) 添加到类别表中
Started POST "/stories" for 127.0.0.1 at 2012-02-10 17:32:56 -0600
Processing by StoriesController#create as HTML
story"=>{"industry_id"=>"8", "user_id"=>"8"}, "category"=>{"category"=>"99"}, "commit"=>"Create Story"}
【问题讨论】:
-
Story
belongs_to分类吗?协会是如何布局的? -
是的:
class Story < ActiveRecord::Base has_many :industries has_many :categories和class Category < ActiveRecord::Base belongs_to :story -
所以其实故事有_许多类别
标签: ruby-on-rails forms