Forum: Ruby on Rails Evaluating variables in Rails views -- help!

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Matthew R. (Guest)
on 2007-04-17 21:57
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) %></li>
<% 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!
Stephen B. (Guest)
on 2007-04-17 22:05
(Received via mailing list)
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:

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

Hope that helps,

Steve
Bill W. (Guest)
on 2007-04-17 22:13
(Received via mailing list)
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
Matthew R. (Guest)
on 2007-04-17 22:24
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.
Bill W. (Guest)
on 2007-04-17 23:25
(Received via mailing list)
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
jeff (Guest)
on 2007-04-18 00:11
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.
Jeff E. (Guest)
on 2007-04-18 00:15
(Received via mailing list)
excuse me while i reiterate everything everyone has already said ;-P
This topic is locked and can not be replied to.