I''m a newbie and have searched many posts and railscast tutorials and still cant get around this associations thing. I have 2 models, a Hotel (created by scaffolding) and Facility. The point is to associate a facility to a hotel, but since my facility table has all the columns it needs with boolean type), a row in that table would do for one hotel. The thing is, I can''t get it to show, and save/edit/update de facilities. I''ve created a hotel_id colum when creating the model Facility. My code is: Models: class Facility < ActiveRecord::Base belongs_to :hotel attr_accessible :concierge, :hotel_id, :room24h end 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 The controllers for de facility are empty and the hotel are stock from the scaffolding. My form in the view 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 /> Hotel Facilities <%= f.fields_for :facility 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%> Something is probably missing in the hotels.controller but I''ve tried and it still isn''t displaying anything. What could it be? Thanks in advance and sorry for the long post -- 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.
El sábado, 14 de abril de 2012 13:58:56 UTC+2, Ruby-Forum.com User escribió:> > I''m a newbie and have searched many posts and railscast tutorials and > still cant get around this associations thing. > > I have 2 models, a Hotel (created by scaffolding) and Facility. > > The point is to associate a facility to a hotel, but since my facility > table has all the columns it needs with boolean type), a row in that > table would do for one hotel. > > The thing is, I can''t get it to show, and save/edit/update de > facilities. I''ve created a hotel_id colum when creating the model > Facility. My code is: > > Models: > > class Facility < ActiveRecord::Base > > belongs_to :hotel > > attr_accessible :concierge, :hotel_id, :room24h > > end > > > 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 > The controllers for de facility are empty and the hotel are stock from > the scaffolding. > > My form in the view 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 /> > Hotel Facilities > > <%= f.fields_for :facility 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%> > Something is probably missing in the hotels.controller but I''ve tried > and it still isn''t displaying anything. What could it be? Thanks in > advance and sorry for the long post > > -- > Posted via http://www.ruby-forum.com/. >Your :new action in your controller probably needs a "@hotel.build_facility" so your nested form has an object to fill. Then you may want to validate it as mandatory or check if the hotel has a facility on your :edit action for the same reason. Regards. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/dCfmoVmsGM0J. 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.
> Your :new action in your controller probably needs a > "@hotel.build_facility"Apologies for the new thread. 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 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.