【发布时间】:2016-06-10 01:02:19
【问题描述】:
我有附件和类别模型,因此当用户上传文件时,他们可以为该附件选择一个类别。我现在希望类别是静态的。关于如何创建静态类别模型选项的建议?
我现在有这个,但我收到以下错误:undefined method 'title' for Syllabus":String
类别模型
class Category < ActiveRecord::Base
CATEGORY = ['Syllabus', 'Assignments', 'Handouts', 'Lectures', 'Other']
has_many :attachments
end
附件new.html.erb
<%= simple_form_for([@group, @group.attachments.build]) do |f| %>
<%= f.collection_select :category_id, Category::CATEGORY, :id, :title, { promt: "Choose a Category" } %>
<%= f.submit %>
<% end %>
附件模型
class Attachment < ActiveRecord::Base
belongs_to :user
belongs_to :group
belongs_to :category
end
架构
create_table "categories", force: :cascade do |t|
t.string "title"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "attachments", force: :cascade do |t|
t.string "title"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.string "name"
t.integer "group_id"
t.integer "category_id"
end
【问题讨论】:
标签: ruby-on-rails