# Void value expression when accessing constant from view

**URL:** <https://www.ruby-forum.com/t/void-value-expression-when-accessing-constant-from-view/176360>\
**Category:** Rails\
**Created:** [September 29, 2009, 3:35pm UTC](https://www.ruby-forum.com/t/void-value-expression-when-accessing-constant-from-view/176360 "2009-09-29T15:35:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martijn\_Vos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e8c25b/32.png) [@Martijn\_Vos](https://www.ruby-forum.com/u/Martijn_Vos)\
**Post date:** [September 29, 2009, 3:35pm UTC](https://www.ruby-forum.com/t/void-value-expression-when-accessing-constant-from-view/176360/1 "2009-09-29T15:35:03Z")

</div>

I’m working on a Rails app using version 2.3.4. I’ve got a big  
form somewhere that uses nested attributes, but I can’t seem to  
access the constants from the other class.

Here’s basically what I’m doing:

class User \< ActiveRecord::Base  
has\_one :pref  
end

class Pref \< ActiveRecord::Base  
FOO = 1  
end

And in app/views/user.html.erb:

\<% form\_for( :user, :url =\> { :action =\> go\_to, :id =\> id }, :html =\>  
{ :multipart =\> true, :id =\> “user\_form” } ) do |f| %\>  
\<%- fields\_for :email\_pref do |p| -%\>  
\<%= p.radio\_button :bar, Pref::FOO %\>  
\<%= p.label :bar, “Bar”, :value =\> Pref::FOO %\>  
…  
\<%- end -%\>  
…  
\<%- end -%\>

Of course there’s quite a bit more code than that, but this is where  
it goes  
wrong. My error is:

/app/models/pref.rb:59: void value expression

Below it the code of the view, pointing out an error in the first line  
that uses Pref::FOO  
I checked if it really is Pref::FOO that’s the problem by adding \<%=  
Pref::FOO %\> before  
the radiobutton. Also, User::FOO (and declaring the constant in the  
User class) works  
perfectly fine. So it looks like I can’t access a Pref constant from a  
User view, but that  
sounds unreasonably limiting to me. I also tried ::Pref::FOO, which  
also doesn’t work.  
I really do want this constant in the Pref class. Putting it in User  
is not a good  
solution for me.

Any idea why this is a problem and how to solve it?

And what does this mysterious “void value expression” mean? Googling  
it yields very few  
results (and most of them are the same).

mcv.

---

<div class="post-metadata">

**Author:** ![Martijn\_Vos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e8c25b/32.png) [@Martijn\_Vos](https://www.ruby-forum.com/u/Martijn_Vos)\
**Post date:** [September 29, 2009, 3:53pm UTC](https://www.ruby-forum.com/t/void-value-expression-when-accessing-constant-from-view/176360/2 "2009-09-29T15:53:58Z")

</div>

Perhaps in your users\_controller action(s), you should create an  
instance of the Pref class for the view to work with?

---

<div class="post-metadata">

**Author:** ![Martijn\_Vos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e8c25b/32.png) [@Martijn\_Vos](https://www.ruby-forum.com/u/Martijn_Vos)\
**Post date:** [September 29, 2009, 4:50pm UTC](https://www.ruby-forum.com/t/void-value-expression-when-accessing-constant-from-view/176360/3 "2009-09-29T16:50:37Z")

</div>

On 29 Sep 2009, at 15:53 PM, Ar Chron wrote:

> Perhaps in your users\_controller action(s), you should create an  
> instance of the Pref class for the view to work with?

Why should I need an instance? Aren’t constants tied to the class  
rather than an instance?

mcv.

---

<div class="post-metadata">

**Author:** ![Martijn\_Vos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e8c25b/32.png) [@Martijn\_Vos](https://www.ruby-forum.com/u/Martijn_Vos)\
**Post date:** [September 29, 2009, 8:56pm UTC](https://www.ruby-forum.com/t/void-value-expression-when-accessing-constant-from-view/176360/4 "2009-09-29T20:56:25Z")

</div>

From [http://www.rubycentral.com/pickaxe/language.html](http://www.rubycentral.com/pickaxe/language.html)

Constants defined within a class or module may be accessed unadorned  
anywhere within the class or module.

Outside the class or module, they may be accessed using the scope  
operator, ``::‘’ prefixed by an expression that returns the appropriate  
class or module object. Like:

class Const  
def getConst  
CONST  
end  
CONST = OUTER\_CONST + 1  
end

Const.new.getConst Â» 100  
Const::CONST Â» 100
