Access model-specific constants in a Rails view
You should access them as following:
Challenge::CLOSED
Since your CLOSED
constant is defined within a class, you need to access the constant using the scope resolution operator. So if your view you would check it like:
# challenge/_details.html.erb<% if @challenge.status == Challenge::CLOSED %> Challenge is closed, broheim!<% end %>
It's a really bad idea to code this kind of statements: your object must handle it's own logic.Imagine if someday you decide to merge status, would you change every conditional in your codebase? No, you should use one method which handles the logic.
I'd do the following:
class Challenge < ActiveRecord::Base SUGGESTED = 0 APPROVED = 1 OPEN = 2 VOTING = 3 CLOSED = 4 #defines: # - suggested? # - approved? # - ... %w(suggested approved open voting closed).each do |state| define_method "#{state}?" do status == self.class.const_get(state.upcase) end end #if you prefer clarity, define each method: def suggested? status == SUGGESTED end #etc...end
Then in your view:
<% if @challenge.closed? %>