Evaluating variables in Rails views -- help!

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!

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,

    Steve

  • 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

    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.

    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

    excuse me while i reiterate everything everyone has already said :stuck_out_tongue_winking_eye:

    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.