【发布时间】:2015-03-23 18:55:37
【问题描述】:
如何将选择 ID 放入数据库中?
我对 ruby on rails 有点陌生。我正在尝试设置一个表单,该表单具有从出生地数据库填充的出生地组合框。
数据库:出生地
Id birthplace created_at updated_at
1 New York ------------- -------------
2 London ------------- -------------
表单提交应以用户在组合框中选择的 id 进入员工数据库。 例如,用户将 Nick 与出生地组合框选择 New York 放在一起,那么它应该是这样的
数据库:员工
Id name birthplace created_at updated_at
1 Nick 1 ------------- -------------
代码:new.html.erb `
<%= form_for(@employees) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :birthplace %>
<%= select("post", :birthplace, @birthplace.collect {|p| [ p.birthplace, p.id ]}, {:include_blank => 'Please Select'} )%>
<%= f.submit "Save", class: "btn btn-large btn-primary" %>
<% end %>
'
代码:employees_controller.rb
Def new
@employees =Employee.new
@birthplace = Birthplace.all
end
def create
@birthplace = Birthplace.all
@employees = Employee.new (params[:employees])
if @student.save
flash[:success]= "Welcome to AVIS!"
render 'new'
else
flash[:success]= "Some Errors!"
render 'new'
end
end
【问题讨论】:
-
不是 student.save 而是 employees.save
-
你试过把
@student.save改成@employees.save -
是的,实际上我在发布问题时打错了。
标签: ruby-on-rails