Topic: Question about a for loop in a has_many relationship

Sorry for the ultra-newbie question here, but I've been butting my head against this for a couple of days.

To teach myself RoR, I am working on a simple framework for a multiplayer online game. Because a user must log in with a password and such, I followed Sonjaya Tandon's "How to build a secured web application with Ruby on Rails." All it does is set up a simple "users" table with a salted-hashed password, which seemed to work pretty well for what I was wanting, and it's working swimmingly.

Now, I'd like to extend that so that a user can have many games. I have a games table, with the following tables: id, user_id, game. (I'm trying to keep it simple at this point.) What I would like is for when you log into the system, your current games are displayed.

I've linked the tables in my models: [code=user.rb]class User < ActiveRecord::Base
  has_many :games
end[/code] [code=game.rb]class Game < ActiveRecord::Base
  belongs_to :user
end[/code]

I've tried everything I can think of to show a list of the currently logged in user's games, to no avail. Here's my latest try: [code=index.rhtml]<h1>Welcome! <%= @user.username %> </h1>

<%= for @game in @user.games %>
<%=   link_to @game.name, :controller => 'games', :action => 'show', :id => id %>
<%= end %>[/code] Without the for-loop, the page displays the @user.username just fine. What am I doing wrong, that I can show a detail of the user's games? Currently, it's erroring out:

compile error
../config/../app/views/workbench/index.rhtml:11: parse error, unexpected ')', expecting kDO_COND or ':' or '\n' or ';'
_erbout.concat(( for @game in @user.games ).to_s); _erbout.concat "\n"
                                           ^
../config/../app/views/workbench/index.rhtml:14: parse error, unexpected kEND
_erbout.concat(( end ).to_s); _erbout.concat "\n"

Could some kind soul out there in Rails-land help me out? Thanks in advance.

Last edited by Yekrats (2006-12-10 09:27:15)

Re: Question about a for loop in a has_many relationship

There is two ways to embed ruby code into an rhtml file. One includes its output into the file, the other is just a way to call arbitrary code, for looping and such.

<%=   some_ruby_code %>    <--- the result of that code will be included in the html. notice the '=' at the beginning: "<%=", this is called an "output block"

<%  some_other_ruby_code %>   <--- the result of this will NOT be included in the html, it's just arbitrary code. Notice the lack of an '=' at the beginning, "<%", this is called an "evaluation block"


your biggest problem is you have declared your loop in an output block, it needs to be in an evaluation block

<% for game in @user.games %>
<%=   link_to game.name, :controller => 'games', :action => 'show', :id => game.id %>
<% end %>

Note how the first and last line now use <% ... %> instead of <%= ... %>

Your other problem is probably ":id => id", where did "id" come from? You probably meant "game.id". It's also legal to just write ":id => game". I also made the game variable local, it doesn't need to be a member variable (dropped the ampersand off of it, no real harm if it does have the ampersand though)

Last edited by tortoise (2006-12-10 11:04:35)

Re: Question about a for loop in a has_many relationship

Thanks a million! The light goes on!

One minor question, though. When you said:

(dropped the ampersand off of it, no real harm if it does have the ampersand though)

Did you mean "at-sign" instead of "ampersand"? If so, then I get it.

Last edited by Yekrats (2006-12-10 11:34:51)