2013-02-12 21 views
0

我有一个表单,用户从下拉列表中选择一个类别。这是在我看来代码:collection_select行为不端

<%= 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 
+0

Project_Category?那是什么? – 2013-02-13 01:30:22

回答

0

你们是不是要分配给每个项目的类别?如果你的关系是正确设置它应该是这样的:

<%= collection_select(:category_id, Category.all, :id, :name) %> 

Project模型将需要一个:category_id整数。