ユーザーがドロップダウン リストからカテゴリを選択するフォームがあります。これが私の見解のコードです:
<%= 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