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!
First I created a Model “profile” which belongs to a User and has many
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.
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…
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.
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
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.