# Evaluating variables in Rails views -- help!

**URL:** <https://www.ruby-forum.com/t/evaluating-variables-in-rails-views-help/92281>\
**Category:** Rails\
**Created:** [April 17, 2007, 7:57pm UTC](https://www.ruby-forum.com/t/evaluating-variables-in-rails-views-help/92281 "2007-04-17T19:57:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mroutley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5f9b8f/32.png) [@mroutley](https://www.ruby-forum.com/u/mroutley)\
**Post date:** [April 17, 2007, 7:57pm UTC](https://www.ruby-forum.com/t/evaluating-variables-in-rails-views-help/92281/1 "2007-04-17T19:57:05Z")

</div>

I’m building my first Rails application, and things are going OK for the  
most part except for stupid little stuff like this that I simply can’t  
figure out (it’s always the little things, isn’t it?).

\<% for p in @pages %\>  
\<li\<% if (params[:id] == p.id) %\> class=“current”\<% end %\>\>\<%=  
link\_to(p.name.capitalize, :action =\> ‘edit’, :id =\> p) %\>  
\<% end %\>

As you can see, I want a CSS class echoed when the current ID (from  
params[:id]) is the same as the ID of the page in the iteration.  
However, the if(params[:id] == p.id) never seems to evaluate to true. I  
have output both params[:id] and p.id in the view, and even when they’re  
both outputting the same number it never evaluates to true.

What am I missing? Is there a better way to do this? Thanks!

---

<div class="post-metadata">

**Author:** ![mroutley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5f9b8f/32.png) [@mroutley](https://www.ruby-forum.com/u/mroutley)\
**Post date:** [April 17, 2007, 8:05pm UTC](https://www.ruby-forum.com/t/evaluating-variables-in-rails-views-help/92281/2 "2007-04-17T20:05:05Z")

</div>

Hey,

This is because you are comparing a string to an integer - params[:id]  
will be a string whereas p.id will be an integer.

The quick way around this is: if params[:id] == p.id.to\_s

… but i’d recommend abstracting this to a helper. Something like  
this:

in application helper:

def class\_if\_current(css\_class, model)  
if params[:id] == model.id.to\_s  
“class=”#{css\_class}" "  
end  
end

then in your view:

- Hope that helps,

---

<div class="post-metadata">

**Author:** ![mroutley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5f9b8f/32.png) [@mroutley](https://www.ruby-forum.com/u/mroutley)\
**Post date:** [April 17, 2007, 8:13pm UTC](https://www.ruby-forum.com/t/evaluating-variables-in-rails-views-help/92281/3 "2007-04-17T20:13:07Z")

</div>

Hi Matthew,

Matthew R. wrote:

> the if(params[:id] == p.id) never seems to evaluate to true.
> 
> What am I missing? Is there a better way to do this? Thanks!

You do not have access to the params hash in the browser. The params  
hash  
sends user-entered data to the server and is constructed by the browser  
when  
a request is sent. The inverse mechanism in Rails is instance  
variables.  
They are used by Rails to construct the page sent by the server to  
respond  
to the browser’s request.

You haven’t included enough code to give specific advice, so here’s the  
generality. If the value you’re looking for is something that was  
passed to  
the server in the last request, after saving it in either the database  
or a  
session variable if you’re going to need it later, assign it to an  
instance  
variable so you’ll have access to it in the view in this cycle. If the  
value you’re looking for is something the user entered on the page in  
this  
cycle, then you’ll need to access it via it’s DOM address, possibly  
using  
javascript.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![mroutley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5f9b8f/32.png) [@mroutley](https://www.ruby-forum.com/u/mroutley)\
**Post date:** [April 17, 2007, 8:24pm UTC](https://www.ruby-forum.com/t/evaluating-variables-in-rails-views-help/92281/4 "2007-04-17T20:24:55Z")

</div>

Stephen B. wrote:

> Hope that helps,
> 
> Steve

Steve,

Thank you, that’s perfect!

@Bill:

I appreciate your reply, but I think you may have misunderstood what I  
was asking about. This is code in a view. I’m not trying to do this  
post-render on the client side. I’m aware of the client-server behavior.

---

<div class="post-metadata">

**Author:** ![mroutley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5f9b8f/32.png) [@mroutley](https://www.ruby-forum.com/u/mroutley)\
**Post date:** [April 17, 2007, 9:25pm UTC](https://www.ruby-forum.com/t/evaluating-variables-in-rails-views-help/92281/5 "2007-04-17T21:25:20Z")

</div>

Hi Matthew,

Matthew R. wrote:

> This is code in a view.

Which is why I replied as I did. It was my understanding that we did  
not  
have access to the params hash in view code. Just ran a test in my  
sandbox  
and found out I was mistaken. Sorry to have wasted your time.

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![mroutley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5f9b8f/32.png) [@mroutley](https://www.ruby-forum.com/u/mroutley)\
**Post date:** [April 17, 2007, 10:15pm UTC](https://www.ruby-forum.com/t/evaluating-variables-in-rails-views-help/92281/6 "2007-04-17T22:15:24Z")

</div>

excuse me while i reiterate everything everyone has already said 😜

---

<div class="post-metadata">

**Author:** ![mroutley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5f9b8f/32.png) [@mroutley](https://www.ruby-forum.com/u/mroutley)\
**Post date:** [April 17, 2007, 10:11pm UTC](https://www.ruby-forum.com/t/evaluating-variables-in-rails-views-help/92281/7 "2007-04-17T22:11:52Z")

</div>

Bill W. wrote:

> You do not have access to the params hash in the browser.

nothing could be further from the truth. e.g.

\<%= text\_field\_tag ‘foo’, params[:foo] %\>

will render a text field named foo, and populate the value from  
params[:foo] if it exists, else no value.

the problem with “params[:id] == p.id” seemingly not working is most  
likely due to a type mismatch: params[:id] is a string, while p.id is an  
int. “params[:id] == p.id.to\_s” would get the job done, but writing a  
helper as Steve suggeste is the better solution.
