Rails collection_select

时间:2010-06-29 08:32:42

标签: ruby-on-rails select

经理:has_many :interns:through => :assigns
实习生:has_many :managers:through => :assigns

我正在尝试创建一个新的分配记录。这是分配/新视图,其中给定的经过身份验证的实习生正在与经理创建新关联:

# assigns/new.html.erb
<% form_for @assign, :url => {:action => "create"} do |p| %>
  <%= p.label "Select Manager", nil, :class => "label" %>
  <%= collection_select :assign, :manager_id, @managers, :id, :manager_name, options ={:prompt => "Select a manager"}, html_options ={:class =>"listBox", :style => "width:25em;"} %>
  <%= p.hidden_field :intern_id %>
<% end %>

# assigns controller
def new
  @intern = Intern.find(params[:id])
  @assign = Assign.new(:intern_id => @intern.id)
  render :layout => 'centered'
end

def create
  @assign = Assign.new(params[:assign])
  @assign.manager_id = params[:manager_id]
  if @assign.save
    redirect_to :controller => "interns", :action => "home"
  else
    render :action => :new
  end
end

问题是:manager_id = nil。为什么?非常感谢。

2 个答案:

答案 0 :(得分:1)

评论以下或

@assign.manager_id = params[:manager_id]

或替换为

@assign.manager_id = params[:assign][:manager_id]

答案 1 :(得分:1)

您不需要此行

@assign.manager_id = params[:manager_id]

您已经在

之前的行中分配了manager_id
@assign = Assign.new(params[:assign])