void value expression on model

Hello,

I'm having an error I don't understand for several days. I have a class Game, which have 2 attributes : class Game < ActiveRecord::Base   has_many :players   has_many :cards, as: :cardable

Then I create a new instance and access the players it fails with void value expression.

@game = Game.new(game_params); puts "cards" puts @game.cards puts @game.cards.size() puts "players" puts @game.players

It displays: cards 0 players

And it fails with: "void value expression" on the last line.

A few days ago this code worked just fine, I don't know what changed and how to understand how to fix it.

Thank you.

do you know how to tail the development log (I assume not because you are using puts statements instead of logger.debug statements). You probably want to get used to using logger.debug(x.inspect) (where x is the variable you want to look at) for debugging.

Please tail the development log and show us the controller action & parameters (copy and paste the whole thing for us).

I recommend using http://gist.github.com for sharing code.

Also, when asking for help, show us the entire Controller code, beginning with the class definition. (you did not specify if the code is inside of an action)

To debug your problem, you need to know what the value of game_params is, which you did not show us.

-Jason

Thank you Jason for your fast reply. I didn't know about the default logger, I have changed my code. Yes it is in a controller, in the create action: gist:d138a4f2c76bb4b32dd2 · GitHub

The full log: Started POST "/game" for 127.0.0.1 at 2014-09-03 00:14:49 +0200 Processing by GameController#create as HTML   Parameters: {"utf8"=>"V", "authenticity_token"=>"gx7TovN+tZbMxOTc91l5P/5jqviBx eO9vlJQaxJ19No=", "game"=>{"small_blind"=>"1", "big_blind"=>"2", "player_number" =>"2"}, "commit"=>"New game !"} cards #<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_Collect ionProxy_Card:0x3b762e8> 0 players Completed 500 Internal Server Error in 114ms

SyntaxError (C:/site/app/models/player.rb:48: voi d value expression):   app/controllers/game_controller.rb:11:in `create'

  Rendered C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-4.0.2 /lib/action_dispatch/middleware/templates/rescues/_source.erb (42.0ms)   Rendered C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-4.0.2 /lib/action_dispatch/middleware/templates/rescues/_trace.erb (7.0ms)   Rendered C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-4.0.2 /lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (9.0 ms)   Rendered C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-4.0.2 /lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues /layout (214.1ms)

your controller action log shows that you have no params for cards (look where it says "Parameter"

also your game_params method on the controller doesn't permit the cards attribute.

Hello,

Thank you for your remarks but it doesn't seem to be the reason here: "cards" is not in "Parameters" but it successfully accesses the attribute when I log it. I have run another page to display a game (already existing in the db), I have got the same error "void value expression" when trying to access the attribute "players" of the Object "game" (previously it worked fine).

It appears that rails doesn't know about the "players" attribute. Where should I check the definition ?

Thank you for your help, I'm really stuck.

Hello,

A little up for this problem which seems complex since nobody has the answer yet.

You have not quoted the previous message so I had to search back through the thread to find what you were talking about. Remember this is a mailing list not a forum (though you may be accessing via a forum like interface.

Post the start of games.rb (not the methods, just the declarations at the front).

Colin

Colin Law wrote in post #1157596:

Hello,

A little up for this problem which seems complex since nobody has the answer yet.

You have not quoted the previous message so I had to search back through the thread to find what you were talking about. Remember this is a mailing list not a forum (though you may be accessing via a forum like interface.

Post the start of games.rb (not the methods, just the declarations at the front).

Colin

Hello Colin,

Yes I'm using the forum interface, didn't know about the mailing list behind.

Here is my game class definition class Game < ActiveRecord::Base   has_many :players   has_one :board   has_many :cards, as: :cardable

Sorry, I meant Player of course.

Colin

Colin Law wrote in post #1157627:

Sorry, I meant Player of course.

Here it is:

class Player < ActiveRecord::Base   belongs_to :game   has_many :cards, as: :cardable

What happens if you say puts @game.players.inspect

Colin

Colin Law wrote in post #1157676:

Colin Law wrote in post #1157627:

Sorry, I meant Player of course.

Here it is:

class Player < ActiveRecord::Base   belongs_to :game   has_many :cards, as: :cardable

What happens if you say puts @game.players.inspect

Colin

The same result as previously since the @game.players is nil, it fails with "void value expression".

The result for @game.inspect gives me this: #<Game id: 50, status: nil, created_at: "2014-09-15 21:54:14", updated_at: "2014-09-15 21:54:14", big_blind: 2, small_blind: 1, player_number: 2, pot: nil, button: nil, to_call: nil, current_player:

'void value expression' indicates a parsing error - what is on line 48 of your player model?

Ideally, post *all* the code of that model in a gist so it's readable...

Colin Law wrote in post #1157676:

Colin Law wrote in post #1157627:

Sorry, I meant Player of course.

Here it is:

class Player < ActiveRecord::Base   belongs_to :game   has_many :cards, as: :cardable

What happens if you say puts @game.players.inspect

Colin

The same result as previously since the @game.players is nil, it fails with "void value expression".

You would not get the error if it were nil. nil.inspect is a perfectly valid statement.

Post the full stack trace of the error along with the relevant bit of source (the version with puts @game.players.inspect) and give us a line number.

What versions of ruby and rails are you running?

Also try going back through the versions of code till you find one that did not show the problem and see what changes you made. Something must have changed to make it behave differently. Either in the code or the running environment (version of ruby and so on).

Colin

This is not a runtime error, this is a syntax error in app/models/player.rb. It’s happening at runtime because that line autoloads the Player class.

You’ll want to check the code before and after line 48 for things that aren’t correctly structured, or post it here.

–Matt Jones

I really like my IDE which shows me syntax errors hi-lighted in red. Any good IDE (Sublime, Rubymine, etc) should do this for you and it’s a good thing to adopt such a tool.

Hello,

Thank you very much everybody, this was as simple as you told: parse error in player.rb.

I wasn't looking in the good direction at all and was confused by the fact it was thrown while accessing the attribute (and then initializing the class).