collection_select行为不端

时间:2013-02-12 19:29:42

标签: html ruby-on-rails-3 forms selection

我有一个表单,用户从下拉列表中选择一个类别。以下是我视图中的代码:

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

1 个答案:

答案 0 :(得分:0)

您是否尝试为每个项目分配一个类别?如果你的关系设置正确,它应该是这样的:

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

您的Project模型需要:category_id整数。