2013-02-12 19 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整數。