初學者的運轉軌道3.2.2,1.8.7紅寶石Ruby on Rails的 - 質量分配問題
我有2個型號,一個酒店(腳手架創建)和設施(空控制器)。 我能夠設置1對1關聯和siplaying字段,但似乎無法將其插入到數據庫中。 即時得到:
ActiveModel::MassAssignmentSecurity::Error in HotelsController#create
Can't mass-assign protected attributes: @hotel
app/controllers/hotels_controller.rb:48:in 'new'
app/controllers/hotels_controller.rb:48:in 'create'
- >我的車型有:(?它可以是,對因爲即時通訊與酒店關聯)
class Hotel < ActiveRecord::Base
has_one :facility, :dependent => :destroy
accepts_nested_attributes_for :facility, :allow_destroy => true
attr_accessible :name, :rating, :recommended, :facility_attributes
end
class Facility < ActiveRecord::Base
belongs_to :hotel
attr_accessible :concierge, :hotel_id, :room24h
end
我的設備控制器,就像我說的,是空的 我hotel_controller是創建腳手架後,默認情況下,只有1箇中添加一行:
def new
@hotel = Hotel.new
@hotel.build_facility #-->I added this only, I searched and all i found was this
respond_to do |format|
format.html # new.html.erb
format.json { render :json => @hotel }
end
end
def create
@hotel = Hotel.new(params[:hotel])
respond_to do |format|
if @hotel.save
format.html { redirect_to @hotel, :notice => 'Hotel was successfully created.' }
format.json { render :json => @hotel, :status => :created, :location => @hotel }
else
format.html { render :action => "new" }
format.json { render :json => @hotel.errors, :status => :unprocessable_entity }
end
end
end
Finaly,我的HTML表單是:
<%= form_for(@hotel) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :rating %><br />
<%= f.number_field :rating %>
</div>
<div class="field">
<%= f.label :recommended %><br />
<%= f.check_box :recommended %>
</div>
<br />
<h2>Hotel Facilities</h2>
<%= f.fields_for :@hotel do |facility_fields| %>
<div class="field">
<%= facility_fields.label :room24h, "24h Room Service:" %>
<%= facility_fields.check_box :room24h %>
</div>
<div class="field">
<%= facility_fields.label "Concierge:" %>
<%= facility_fields.check_box :concierge %>
</div>
<%end%>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
我在創建時出現的錯誤是在問題的起始處。我應該添加更多的代碼做hotel_controller嗎?會是什麼呢?另外,爲了編輯/更新,我還需要做什麼更多的代碼?
在此先感謝和抱歉,即時通訊新的在RoR。
仍顯示相同的錯誤:( 更多提示? – Silver 2012-04-15 23:02:47