RubyonRailsアプリでsimple_nested_formを作成しようとしています。フォームを送信すると、フォームにリダイレクトされて再度入力するため、不明なエラーが発生します。フォームを送信したときのRailsサーバーコンソールの出力は次のとおりです。そこにランダムな「0」=>がスローされているようです。
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:machineである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 ..." %>