0

我正在尝试使用一个表单来在医院中创建一个单元,您可以在 UnitShiftTypes 表中选择适用于该单元的 shift_types。我在 Units 和 ShiftTypes 之间有 has_many 和 :through UnitShiftTypes。似乎每次都会抛出错误。我似乎无法弄清楚这一点。任何帮助将不胜感激!

_form.html.erb

<%= simple_form_for(@unit) do |f| %>
  <%= f.error_notification %>
  <div class="form-inputs">
    <%= f.input :name %>
    <%= f.input :number %>
    <%= f.fields_for :unit_shift_type do |ff| %>
      <%= f.select :shift_type_ids, ShiftType.all.collect {|x| [x.name, x.id, ]}, {}, :multiple => true %>
      <%= ff.hidden_field :user_id, :value => current_user %>
    <% end %>
    <%= f.input :hospital_id %>
  </div>
  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

单位.rb

class Unit < ActiveRecord::Base
belongs_to :hospital
has_many :unit_users, :dependent => :restrict
has_many :unit_shift_types
has_many :users, :through => :unit_users, :dependent => :restrict
has_many :shift_types, :through => :unit_shift_types

attr_accessible :hospital_id, :name, :number, :unit_id, :unit_shift_types_attributes
accepts_nested_attributes_for :unit_shift_types

validates_presence_of :hospital_id, :name, :number
validates_uniqueness_of :number, :scope => :hospital_id, :message => "is already associated with this Hospital."
end

unit_shift_type.rb

class UnitShiftType < ActiveRecord::Base
belongs_to :shift_type
belongs_to :unit

attr_accessible :shift_type_id, :unit_id

validates_presence_of :shift_type_id, :unit_id
validates_uniqueness_of :shift_type_id, :scope => :unit_id, :message => "is already associated with this Unit."
end

shift_type.rb

class ShiftType < ActiveRecord::Base
has_many :unit_shift_types

attr_accessible :created_by_id, :enabled, :end_time, :name, :start_time
validates_presence_of :start_time, :end_time, :name, :created_by_id

end

units_controller.rb

# POST /units
# POST /units.json
def create
  @unit = Unit.new(params[:unit])

  respond_to do |format|
    if @unit.save
      format.html { redirect_to @unit, notice: 'Unit was successfully created.' }
      format.json { render json: @unit, status: :created, location: @unit }
    else
      format.html { render action: "new" }
      format.json { render json: @unit.errors, status: :unprocessable_entity }
    end
  end
end

# PUT /units/1
# PUT /units/1.json
def update
  @unit = Unit.find(params[:id])

  respond_to do |format|
    if @unit.update_attributes(params[:unit])
      format.html { redirect_to @unit, notice: 'Unit was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: "edit" }
      format.json { render json: @unit.errors, status: :unprocessable_entity }
    end
  end
end

请求参数:

{"utf8"=>"✓", "authenticity_token"=>"CnIZCDbEVNr/B8fby2La8ibvtQtjycwO/BD0mQ2sOw4=", "unit"=>{"name"=>"1", "number"=>"1", "shift_type_ids"=>["", "1"], "unit_shift_type"=>{"user_id"=>""}, "hospital_id"=>"1"}, "commit"=>"Create Unit", "action"=>"create", "controller"=>"units"}
4

2 回答 2

0

如果您只想在创建新单元时添加现有的 shift_types 并且您正在使用 shift_type_ids,那么您不需要指定嵌套属性。无论如何,您正在使用表单对象来创建select,因此无需保留fields_for阻止。虽然您正在使用fields_for创建隐藏字段user_id,但我看不到您在 Model.xml 中的哪里有该user_id属性UnitShiftType

所以替换 fields_for 块

<%= f.fields_for :unit_shift_type do |ff| %>
  <%= f.select :shift_type_ids, ShiftType.all.collect {|x| [x.name, x.id, ]}, {}, :multiple => true %>
  <%= ff.hidden_field :user_id, :value => current_user %>
<% end %>

<%= f.select :shift_type_ids, ShiftType.all.collect {|x| [x.name, x.id]}, {}, :multiple => true %>

并添加shift_type_idsUnit模型的attr_accessible.

于 2013-02-12T22:04:53.317 回答
-1

尝试更换:

<%= f.select :shift_type_ids, ShiftType.all.collect {|x| [x.name, x.id, ]}, {}, :multiple => true %>

和:

  <%= f.select :shift_type_ids, @unit.unit_shift_types.collect {|x| [x.name, x.id, ]}, {}, :multiple => true %>

但要获得真正的解决方案,我们必须查看控制器的创建和/或更新操作。您如何将 shift_type 添加到单元中?

它应该看起来像:

@unit << shift_type
于 2013-02-12T16:17:50.237 回答