Hi! I''ve got a not so unfamiliar multiple models in one form problem. Due to the complex forms railscast I''ve got it up and running for a one-to- many relationship but now I have a one-to-one relationship and I didn''t get it done. Here is my code, perhaps someone can take a look and give me a hint what''s wrong. I put what is going wrong into the code where I think it is belonging to. And I don''t know if it matters but I''m running Rails 2.1.1. ## # Model class Subscription < ActiveRecord::Base belongs_to :user def user=(attributes) ## # !!! Here is something wrong # - I get a Hash with all my fields here # - attributes belongs to class HashWithIndifferentAccess # - Whith self I get the error „undefined method `stringify_keys!''...“ # - Without self the subscription model is saved without the user model self.build_user(attributes) end end class Subscription < ActiveRecord::Base has_one :subscription end ## # Controller class SubscriptionsController < ApplicationController def new @subscription = Subscription.new @subscription.build_user respond_to do |wants| wants.html wants.xml { render :xml => @subscription } end end def create @subscription = Subscription.new(params[:subscription]) respond_to do |wants| if @subscription.save flash[:notice] = ''Subscription was successfully created.'' wants.html { redirect_to root_url } wants.xml { render :xml => @subscription, :status => :created, :location => @payment } else wants.html { render :action => "new" } wants.xml { render :xml => @subscription.errors, :status => :unprocessable_entity } end end end end ## # View (Haml syntax) - form_for :subscription, @subscription, :url => subscriptions_path do |subscription_form| ... subscription fields - subscription_form.fields_for :user do |user_form| ... user fields Every help is appreciated. Thanks in advance. Regards, Stefan --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Sarah Mei
2009-Feb-24 22:05 UTC
Re: Multiple models one form and a one-to-one relationship
In a has_one/belongs_to association, I don''t think you need the usermethod. What happens if you take it out completely? On Tue, Feb 24, 2009 at 1:27 PM, Stefan Frede <sfrede-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi! > > I''ve got a not so unfamiliar multiple models in one form problem. Due > to the complex forms railscast I''ve got it up and running for a one-to- > many relationship but now I have a one-to-one relationship and I > didn''t get it done. > > Here is my code, perhaps someone can take a look and give me a hint > what''s wrong. I put what is going wrong into the code where I think it > is belonging to. And I don''t know if it matters but I''m running Rails > 2.1.1. > > ## > # Model > > class Subscription < ActiveRecord::Base > belongs_to :user > > def user=(attributes) > ## > # !!! Here is something wrong > # - I get a Hash with all my fields here > # - attributes belongs to class HashWithIndifferentAccess > # - Whith self I get the error „undefined method > `stringify_keys!''...“ > # - Without self the subscription model is saved without the user > model > > self.build_user(attributes) > end > end > > class Subscription < ActiveRecord::Base > has_one :subscription > end > > ## > # Controller > > class SubscriptionsController < ApplicationController > def new > @subscription = Subscription.new > -9/ybXWNEfnWxtqrtir1q5Srx10fhMbqb@public.gmane.org_user > > respond_to do |wants| > wants.html > wants.xml { render :xml => @subscription } > end > end > > def create > @subscription = Subscription.new(params[:subscription]) > > respond_to do |wants| > if @subscription.save > flash[:notice] = ''Subscription was successfully created.'' > wants.html { redirect_to root_url } > wants.xml { render :xml => @subscription, :status > => :created, :location => @payment } > else > wants.html { render :action => "new" } > wants.xml { render :xml => @subscription.errors, :status > => :unprocessable_entity } > end > end > end > end > > ## > # View (Haml syntax) > > - form_for :subscription, @subscription, :url => subscriptions_path do > |subscription_form| > > ... subscription fields > > - subscription_form.fields_for :user do |user_form| > > ... user fields > > Every help is appreciated. Thanks in advance. > > Regards, > Stefan > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Try self.build_user(:name=>attributes) , name or whatever the column is. On Feb 24, 3:27 pm, Stefan Frede <sfr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi! > > I''ve got a not so unfamiliar multiple models in one form problem. Due > to the complex forms railscast I''ve got it up and running for a one-to- > many relationship but now I have a one-to-one relationship and I > didn''t get it done. > > Here is my code, perhaps someone can take a look and give me a hint > what''s wrong. I put what is going wrong into the code where I think it > is belonging to. And I don''t know if it matters but I''m running Rails > 2.1.1. > > ## > # Model > > class Subscription < ActiveRecord::Base > belongs_to :user > > def user=(attributes) > ## > # !!! Here is something wrong > # - I get a Hash with all my fields here > # - attributes belongs to class HashWithIndifferentAccess > # - Whith self I get the error „undefined method > `stringify_keys!''...“ > # - Without self the subscription model is saved without the user > model > > self.build_user(attributes) > end > end > > class Subscription < ActiveRecord::Base > has_one :subscription > end > > ## > # Controller > > class SubscriptionsController < ApplicationController > def new > @subscription = Subscription.new > @subscription.build_user > > respond_to do |wants| > wants.html > wants.xml { render :xml => @subscription } > end > end > > def create > @subscription = Subscription.new(params[:subscription]) > > respond_to do |wants| > if @subscription.save > flash[:notice] = ''Subscription was successfully created.'' > wants.html { redirect_to root_url } > wants.xml { render :xml => @subscription, :status > => :created, :location => @payment } > else > wants.html { render :action => "new" } > wants.xml { render :xml => @subscription.errors, :status > => :unprocessable_entity } > end > end > end > end > > ## > # View (Haml syntax) > > - form_for :subscription, @subscription, :url => subscriptions_path do > |subscription_form| > > ... subscription fields > > - subscription_form.fields_for :user do |user_form| > > ... user fields > > Every help is appreciated. Thanks in advance. > > Regards, > Stefan--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Stefan Frede
2009-Feb-24 22:30 UTC
Re: Multiple models one form and a one-to-one relationship
Without the user method in the subscription model I get the following error: User(#17369790) expected, got HashWithIndifferentAccess(#2700040) I''ve changed the user method: def user=(attributes) attributes.each do |attribute| self.build_user(attribute.first => attribute.last) end end I get no error and the subscripton model is saved, but not the user model. These are my parameters: ..."subscription"=>{ "user"=>{ "name"=>"vorname", "city"=>"ort", "postal_code"=>"plz", "street"=>"straße", "phone"=>"telefon", "c_o"=>"c/o (optional)", "surname"=>"nachname", "comment"=>"kommentar (optional)", "email"=>"e-mail" } }... For me they are not looking that bad. On 24 Feb., 23:07, Me <chabg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Try self.build_user(:name=>attributes) , name or whatever the column > is. > > On Feb 24, 3:27 pm, Stefan Frede <sfr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi! > > > I''ve got a not so unfamiliar multiple models in one form problem. Due > > to the complex forms railscast I''ve got it up and running for a one-to- > > many relationship but now I have a one-to-one relationship and I > > didn''t get it done. > > > Here is my code, perhaps someone can take a look and give me a hint > > what''s wrong. I put what is going wrong into the code where I think it > > is belonging to. And I don''t know if it matters but I''m running Rails > > 2.1.1. > > > ## > > # Model > > > class Subscription < ActiveRecord::Base > > belongs_to :user > > > def user=(attributes) > > ## > > # !!! Here is something wrong > > # - I get a Hash with all my fields here > > # - attributes belongs to class HashWithIndifferentAccess > > # - Whith self I get the error „undefined method > > `stringify_keys!''...“ > > # - Without self the subscription model is saved without the user > > model > > > self.build_user(attributes) > > end > > end > > > class Subscription < ActiveRecord::Base > > has_one :subscription > > end > > > ## > > # Controller > > > class SubscriptionsController < ApplicationController > > def new > > @subscription = Subscription.new > > @subscription.build_user > > > respond_to do |wants| > > wants.html > > wants.xml { render :xml => @subscription } > > end > > end > > > def create > > @subscription = Subscription.new(params[:subscription]) > > > respond_to do |wants| > > if @subscription.save > > flash[:notice] = ''Subscription was successfully created.'' > > wants.html { redirect_to root_url } > > wants.xml { render :xml => @subscription, :status > > => :created, :location => @payment } > > else > > wants.html { render :action => "new" } > > wants.xml { render :xml => @subscription.errors, :status > > => :unprocessable_entity } > > end > > end > > end > > end > > > ## > > # View (Haml syntax) > > > - form_for :subscription, @subscription, :url => subscriptions_path do > > |subscription_form| > > > ... subscription fields > > > - subscription_form.fields_for :user do |user_form| > > > ... user fields > > > Every help is appreciated. Thanks in advance. > > > Regards, > > Stefan--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---