On Feb 10, 2012, at 5:23 AM, Christopher Jones wrote: >> And just to confirm, you are doing this in the user controller, right? > > Yes this is all happening in the user model > >> What do your models look like for wishlist and user then? If you have >> defined the has_many belongs_to relationship, then the one-liner I >> showed you: > > My user model has a: has_one: wishlist > My wishlist model: belongs_to: user > >> @wishlist = current_user.wishlists.create(params[:wishlist]) >> >> should create the relationship and build and save the wishlist. > > I have put as you said: > > before_filter :set_wishlist, :only => :create > > def set_wishlist > current_user.wishlists.create(params[:wishlist]) > end > > But now I have errors return back stating: > > undefined method `wishlists' for nil:NilClass > > If I change it to wishlist I get the same error as well as Wishlist. If > I state params in the def set_wishlist(params) it returns aargument (0 > of 1) error.
Okay, then you have another issue. For some reason, current_user isn't in the world when the method is being called, or maybe it's not in scope within that method. Can you put a breakpoint inside the method and see what current_user equals at that point? Are you using Devise or Authologic or similar, and are you sure the user is assigned (logged in) at the moment that this method runs? I didn't try assigning a variable from outside of the method when I did my little test (quoted earlier), just did a static assignment, and it worked. We need to figure out how to get current_user into scope where you need it, and then you can have the same success. Walter > > -- > 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 [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > -- 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 [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

