Add Objects to personal List

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

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'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

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 :confused: