0

我試圖讓單個窗體在醫院中創建一個單元,您可以選擇適用於UnitShiftTypes表中該單元的shift_types。我有has_many和:通過單位和ShiftType之間的UnitShiftTypes。它似乎每次都會拋出一個錯誤。我似乎無法弄清楚這一點。任何幫助將不勝感激!無法批量分配受保護的屬性:shift_type_ids,unit_shift_type

_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 %> 

unit.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"} 

回答

-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 %> 

但要得到真正的解決方案,我們」你必須看到你r控制器的創建和/或更新操作。你如何爲單元添加shift_type?

它應該是這個樣子:

@unit << shift_type 
+0

我已經用控制器#create和#update更新了我的問題。你提供的解決方案的問題是我試圖抓住先前構建的ShiftType並將它們關聯到單元。 我實際上並未在單位內保存ID參考。我將爲每個選定的項目創建一個UnitShiftType記錄。所以如果他們選擇了Day,Night ShiftTypes。他們在UnitShiftType內有2個與Unit#關聯的記錄。 – EricC 2013-02-12 16:29:36

0

如果你只是想在創建一個新的單位,以增加現有shift_types和你使用的是shift_type_ids那麼你並不需要指定嵌套屬性。無論如何,你使用的表單對象來創建select,所以沒有必要保持fields_for塊。雖然您正在使用fields_for創建隱藏字段user_id,但我無法看到UnitShiftType模型中的user_id屬性在哪裏。

因此,與

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

更換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 %> 

並添加shift_type_idsUnit型號的attr_accessible

相關問題