Hello, I have 2 models, city and tip. A city an have only one tip. So what i am trying to do is to add my tip properties to the city form and create or update the city with his child the tip. but what i''ve done so far isn''t working... I have this error: undefined method `build'' for nil:NilClass Model City: class City < ActiveRecord::Base attr_accessible :name, :continent, :tip_attribute has_one :shop belongs_to :city_preferences belongs_to :tip validates_presence_of :name,:continent def tip_attribute tip end def tip_attribute=(attribute) tip.build(attribute) end end City form view: <% form_for @city do |f| %> <%= f.error_messages %> <p> <%= f.label :name %><br /> <%= f.text_field :name %> </p> <p> <%= f.label :continent %><br /> <%= f.text_field :continent %> </p> <p><%= f.label :Tip %></p> <%fields_for "city[tip_attribute]", @city.tip do |t| %> <p>Min <%= t.text_field :min %><br /> <p>Max <%= t.text_field :max %> <%end%> <p><%= f.submit "Submit" %></p> <% end %> Greg -- 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 Mar 9, 6:31 am, Greg Ma <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello, > I have 2 models, city and tip. A city an have only one tip. So what i am > trying to do is to add my tip properties to the city form and create or > update the city with his child the tip. > but what i''ve done so far isn''t working... I have this error: undefined > method `build'' for nil:NilClass >> def tip_attribute=(attribute) > tip.build(attribute) > endIf you have a has_many you can do tips.build(...) but with belongs_to and has_one you need to call build_tip instead Fred -- 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.
Frederick Cheung wrote:> If you have a has_many you can do tips.build(...) but with belongs_to > and has_one you need to call build_tip instead > > FredThanks Fred, it works better now. But an have another issue. Now the data are stored in the database but the column city_it in the table tips is nil. Do i have the set it the setter like this? def tip_attribute=(attribute) attribute[:city_id] = self.id build_tip(attribute) end Tip model: class Tip < ActiveRecord::Base attr_accessible :min,:max,:city_id has_one :city validates_numericality_of :min, :only_integer => true validates_numericality_of :max, :only_integer => true end It does work if I do this, but is it the correct way to do it? should it be filled automaticly by active record? Greg -- 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.