I posted a questionnot long ago, yet I still cannot get pass this simple issue. Begginer Running rails 3.2.2, Ruby 1.8.7 I have 2 models, a Hotel (created by scaffolding) and Facility (with empty controller). I am able to set the 1-to-1 association and siplaying fields but can''t seem to insert it in the databases. Im getting: 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'' --> My models are: 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 My facility controller, as I said, is empty (it can be, right? since im associating with Hotel?) My hotel_controller is the default after creating scaffolding, only with 1 added line: 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, my form html is: <%= 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 %> The error I am getting when creating is at beggining of question. Should I be adding more code do the hotel_controller? What could it be? Also, the facility_controller can be empty right? I mean, I''m adding all the data from the hotel_controller. Thanks in advance and sorry, im new at RoR :( -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 15 April 2012 23:10, Miguel A. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I posted a questionnot long ago, yet I still cannot get pass this simple > issue. > > Begginer Running rails 3.2.2, Ruby 1.8.7 > > I have 2 models, a Hotel (created by scaffolding) and Facility (with > empty controller). I am able to set the 1-to-1 association and siplaying > fields but can''t seem to insert it in the databases. Im getting: > > 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'' > --> My models are: > > 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 > My facility controller, as I said, is empty (it can be, right? since im > associating with Hotel?) My hotel_controller is the default after > creating scaffolding, only with 1 added line: > > def new > @hotel = Hotel.new > -ElbK3ZHsHofypzumFFeuLQ@public.gmane.org_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, my form html is: > > <%= 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| %>The problem lies here I think, it should possibly be fields_for :facility do ... Colin> <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 %> > The error I am getting when creating is at beggining of question. Should > I be adding more code do the hotel_controller? What could it be? Also, > the facility_controller can be empty right? I mean, I''m adding all the > data from the hotel_controller. > > > Thanks in advance and sorry, im new at RoR :( > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- gplus.to/clanlaw -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> The problem lies here I think, it should possibly be fields_for > :facility do ...Apologies for the new thread. It now shows up in the form, the problem now is when I submit, it gives out an error. If i change to what you suggested, the same error apears, but for the @facility ActiveModel::MassAssignmentSecurity::Error in HotelsController#create Can''t mass-assign protected attributes: @facility app/controllers/hotels_controller.rb:60:in `new'' app/controllers/hotels_controller.rb:60:in `create'' As for the code, I even tried to do this on the hotel_controller: def new @hotel = Hotel.new @facility = @hotel.build_facility end def create @hotel = Hotel.new(params[:hotel]).Facility.new(params[:facility]) @hotel.save end But with no success, still mass-assignement error Display log: Started POST "/hotels" for 127.0.0.1 at Mon Apr 16 13:57:18 +0100 2012 Processing by HotelsController#create as HTML Parameters: {"hotel"=>{"@facility"=>{"concierge"=>"0", "room24h"=>"1"}, "rating"=>"2", "recommended"=>"0", "name"=>"kjo"}, "utf8"=>"✓", "commit"=>"Create Hotel", "authenticity_token"=>"buHOvKyQup/ljrFcUej8mzZ01DEJGxYqaUMvUUkyjn8="} Completed 500 Internal Server Error in 0ms ActiveModel::MassAssignmentSecurity::Error (Can''t mass-assign protected attributes: @facility): app/controllers/hotels_controller.rb:60:in `new'' app/controllers/hotels_controller.rb:60:in `create'' Rendered c:/Ruby187/lib/ruby/gems/1.8/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms) Rendered c:/Ruby187/lib/ruby/gems/1.8/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (2.0ms) Rendered c:/Ruby187/lib/ruby/gems/1.8/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (29.0ms) I thank you for your time to help me. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Problem solved. To anyone who has the same issue, added to the new controller: @facility=@hotel.build_facility And in the nested form, the correct syntax is: <%= f.fields_for :facility do |fc|%> Instead of ... :@facility do ... -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.