我想在我的Ruby on Rails应用程序中构建一个simple_nested_form。当我提交表单时,我收到了一些未知的错误,因为它只是重定向到表单以再次输入。这是我在提交表单时在rails服务器控制台中的输出。看起来有一些随机的“0”=>在那里抛出。无法获取simple_nested_form来提交数据
Parameters: {"machine"=>{"name"=>"2134", "ip_adress"=>"2", "machine_employees_attributes"=>{"0"=>{"machine_id"=>"1", "employee_id"=>"2"}}}, "commit"=>"Create Machine"}
我有一台机器模型,该模型的has_many:machine_employees
和belongs_to的一个machineemployee模型:机
你有为什么这个0 =>可能出现的任何想法,因为我认为这是什么是给我的问题。
这里是我的模型的代码。
机
class Machine < ActiveRecord::Base
# Relationships
has_many :machine_employees
has_many :employees, :through => :machine_employees
accepts_nested_attributes_for :machine_employees, :reject_if => lambda{ |me| me[:employee_id].blank? }
attr_accessible :ip_adress, :name, :machine_employees_attributes
# Validations
validates_presence_of :name, :ip_adress
end
MachineEmployee
class MachineEmployee < ActiveRecord::Base
before_validation :set_default
# Relationships
belongs_to :machine
belongs_to :employee
attr_accessible :employee_id, :machine_id, :end_date, :start_date
# Validations
validates_presence_of :employee_id, :machine_id, :start_date
private
# Callback Methods
def set_default
self.start_date = Date.today
self.end_date = nil
end
end
新机形式
<div class="row-fluid">
<div class="span3">
<h1>Add a Machine</h1>
<br />
<%= simple_nested_form_for @machine do |f| %>
<%= render "machine_fields", :f => f %>
<%= f.button :submit %>
<%= link_to 'Back', machines_path %>
</div>
<div class="span4">
<h4>Assign an Employee to This Machine</h4>
<%= f.simple_fields_for :machine_employees do |me_form| %>
<!-- render nested machine_employee fields-->
<%= render "machine_employee_fields", :f => me_form %>
<% end %>
</div>
<% end %>
</div>
机员工字段部分
<%= f.input :machine_id, :as => :hidden, :input_html => { :value => @machine.id } %>
<%= f.input :employee_id, collection: @employees, :id => :name, :prompt => "Select ..." %>
这听起来像某些东西不验证这是为什么它不会保存。您可以发布您的模型和/或您尝试提交表单时获得的控制台输出吗? – 2013-02-24 02:15:18