Begginer実行レール3.2.2、Rubyの1.8.7ルビー - マス割り当て問題
私は2つのモデルがあり、(空のコントローラー付き)(足場によって作成された)ホテル施設。 1対1の関連付けフィールドとsiplayingフィールドは設定できますが、データベースに挿入できないようです。 Imが取得:
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
マイ設備コントローラ、私が言ったように空である(それはイムホテルと会合するので、右にすることができますか??)
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は次のとおりです:
私hotel_controllerはわずか1つの追加された行で、足場を作成した後、デフォルトであります<%= 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で新しいです。 Facility
同じエラーが表示される:( これ以上のヒント? – Silver