繁体   English   中英

collection_select行为异常

[英]collection_select misbehaving

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

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

您是否要为每个项目分配一个类别? 如果您的人际关系设置正确,则应为:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM