Hey there! I''m developing a Game Database where every User can Add Titles to the list, for everyone to see (like imdb). But Also I want to add game titles to a personal list. How Do I do that? So far everything is working fine. I can show the details of each created game, now I only need an action button that adds this very game title to my personal profile list. First I created a Model "profile" which belongs to a User and has many games. In my gamesController I added the method: def add_to_profile @profile = Game.find(params[:id]) @profile.save end But I don''t know if this is right. What should my routing look like? How do I do this properly? I appreciate any kind of help! -- 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On 8 February 2013 21:37, Ryo Saeba <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hey there! > > I''m developing a Game Database where every User can Add Titles to the > list, for everyone to see (like imdb). But Also I want to add game > titles to a personal list. How Do I do that? > > So far everything is working fine. I can show the details of each > created game, now I only need an action button that adds this very game > title to my personal profile list. > > First I created a Model "profile" which belongs to a User and has many > games. > > In my gamesController I added the method: > > def add_to_profile > @profile = Game.find(params[:id]) > @profile.save > end > > But I don''t know if this is right. What should my routing look like? How > do I do this properly? I appreciate any kind of help!Have a look at the Rails Guide on ActiveRecord Associations. It describes the different associations and how to use them. Also, if you have not already done so, then work right through a tutorial such as railstutorial.org, which is free to use online and will show you the basics of Rails, including how to use associations. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
thanks for replying! I sure know about the associations tutorials, but that''s not my main problem. I assume, that my model connections are fine so far, but I don''t know how to save a models object in another Model. I bet it is quite simple, but I can''t figure it out... -- 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On 9 February 2013 10:58, Ryo Saeba <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> thanks for replying! > > I sure know about the associations tutorials, but that''s not my main > problem. I assume, that my model connections are fine so far, but I > don''t know how to save a models object in another Model. I bet it is > quite simple, but I can''t figure it out...Have another look at the Rails Guide, it covers methods such as build, build_association, create_association and so on. Also have you worked through a good tutorial? I am sure railstutorial.org covers this. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Feb 9, 2013, at 5:58 AM, Ryo Saeba wrote:> thanks for replying! > > I sure know about the associations tutorials, but that''s not my main > problem. I assume, that my model connections are fine so far, but I > don''t know how to save a models object in another Model. I bet it is > quite simple, but I can''t figure it out... >You''re probably trying too hard. If you build an associated record, as Colin hinted, then it is automagically saved when the parent is saved. All of the issues of the ID not existing (if it''s a new record) until the parent is saved are already handled for you. Walter -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
I encountered a problem that has to be solved first, before handling the "add_object"-Feature. There are a few issues with my "User Show" Page. I get a routing error when opening the startpage (because of the link to the user profile) my browser says: Routing Error No route matches {:controller=>"users", :action=>"show"} This only happens in the front page, not on others like "game/:id". But what''s the problem? Also Typing the desired url by hand "users/1" opens the "show"-page. My UsersController has all he needs: def show @user = User.find(params[:id]) end And my Rooting says resources :users, only: [:new, :create, :show] get "users/:id" => "users#show", as: "user" And I got this helper def link_to_userprofile link_to "Userprofile", user_path end As I said, typing the url will open the user profile, and i can get there with the link from any point of my page, except the front page, which throws me the routing error. That doesen''t make any sense :/ -- 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.