Void value expression when accessing constant from view

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.

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

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.

From 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs