void value expression on model

97 views
Skip to first unread message

Adrien R.

unread,
Sep 2, 2014, 4:16:15 PM9/2/14
to rubyonra...@googlegroups.com
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.

--
Posted via http://www.ruby-forum.com/.

Jason Fleetwood-Boldt

unread,
Sep 2, 2014, 5:42:44 PM9/2/14
to rubyonra...@googlegroups.com


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
-- 
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-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f17aacbad6ed08d697bac0440966afb8%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.


Adrien R.

unread,
Sep 2, 2014, 6:20:57 PM9/2/14
to rubyonra...@googlegroups.com
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: https://gist.github.com/anonymous/d138a4f2c76bb4b32dd2

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)

Jason Fleetwood-Boldt

unread,
Sep 3, 2014, 10:47:09 AM9/3/14
to rubyonra...@googlegroups.com

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.
> --
> 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-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/76fb6cc467833a0c12eee3f00729416f%40ruby-forum.com.

Adrien R.

unread,
Sep 9, 2014, 6:40:55 PM9/9/14
to rubyonra...@googlegroups.com
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.

Adrien R.

unread,
Sep 14, 2014, 5:00:10 AM9/14/14
to rubyonra...@googlegroups.com
Hello,

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

Colin Law

unread,
Sep 14, 2014, 8:28:53 AM9/14/14
to rubyonra...@googlegroups.com
On 14 September 2014 09:58, Adrien R. <li...@ruby-forum.com> wrote:
> 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

Adrien R.

unread,
Sep 14, 2014, 6:07:59 PM9/14/14
to rubyonra...@googlegroups.com
Colin Law wrote in post #1157596:
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

Colin Law

unread,
Sep 15, 2014, 2:53:32 AM9/15/14
to rubyonra...@googlegroups.com
Sorry, I meant Player of course.

Colin

Adrien R.

unread,
Sep 15, 2014, 4:16:41 PM9/15/14
to rubyonra...@googlegroups.com
Colin Law wrote in post #1157627:
> Sorry, I meant Player of course.
>

Here it is:

class Player < ActiveRecord::Base
belongs_to :game

Colin Law

unread,
Sep 15, 2014, 4:50:10 PM9/15/14
to rubyonra...@googlegroups.com
On 15 September 2014 21:15, Adrien R. <li...@ruby-forum.com> wrote:
> 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

Adrien R.

unread,
Sep 15, 2014, 5:56:42 PM9/15/14
to rubyonra...@googlegroups.com
Colin Law wrote in post #1157676:
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:
nil>

Hassan Schroeder

unread,
Sep 15, 2014, 6:15:38 PM9/15/14
to rubyonrails-talk
On Tue, Sep 2, 2014 at 3:20 PM, Adrien R. <li...@ruby-forum.com> wrote:

> 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: void value expression):
> app/controllers/game_controller.rb:11:in `create'

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

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan

Colin Law

unread,
Sep 16, 2014, 3:27:15 AM9/16/14
to rubyonra...@googlegroups.com
On 15 September 2014 22:56, Adrien R. <li...@ruby-forum.com> wrote:
> Colin Law wrote in post #1157676:
>> On 15 September 2014 21:15, Adrien R. <li...@ruby-forum.com> wrote:
>>> 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


>
> 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:
> nil>
>
> --
> 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-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/41a4027130c38e69f34fc102ccfd6e3d%40ruby-forum.com.

Matt Jones

unread,
Sep 16, 2014, 11:06:04 AM9/16/14
to rubyonra...@googlegroups.com


On Tuesday, 2 September 2014 18:20:57 UTC-4, Ruby-Forum.com User wrote:
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: https://gist.github.com/anonymous/d138a4f2c76bb4b32dd2

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'


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

Jason Fleetwood-Boldt

unread,
Sep 16, 2014, 12:48:52 PM9/16/14
to rubyonra...@googlegroups.com


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. 


--
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-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.

Adrien R.

unread,
Sep 16, 2014, 5:06:07 PM9/16/14
to rubyonra...@googlegroups.com
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).
Reply all
Reply to author
Forward
0 new messages