Evaluating variables in Rails views -- help!


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:

<li <%= class_if_current("current", p) %>>

Hope that helps,


Hi Matthew,

Matthew Rogers 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