我有一个表单,用户从下拉列表中选择一个类别。这是我认为的代码:
<%= collection_select(:project_categories, :id, Project_Category.all, :id, :category_name) %>
表单中的所有其他字段(是的,collection_select 在表单内)按预期保存到数据库并从数据库中读取。但不是collection_select...
这是模型:
class Project < ActiveRecord::Base
attr_accessible :category,
...
belongs_to :user
has_one :category
...
end
控制器:
def create
@user = current_user
@project = current_user.build_project(params[:project])
@project.save
render 'edit'
end
...
def update
@project = Project.find(params[:id])
@user = current_user
@project.current_step = session[:step]
end
...
private
def correct_user
@project = current_user.project
redirect_to show_user_path if @project.nil?
end
def has_project
@project = current_user.projects.find_by_id(params[:id])
end
end