I’ve created a helper for dealing with tables called TableHelper. I
have a method to deal with sorting asc or desc from within columns.
They involve images and some other transitions. Here’s the code:
def sort_column(title, direction)
(link_to image_tag(“up.gif”, :border=>0),
rushing_offenses_path(:numteams => (@showall ? 120 : nil), :orderby =>
title, :sortby => direction)) if !@searchteams
end
However, I’m trying to keep the code dry and I want to reuse the same
method for all of the models/views that require it. Unfortunately, it
has a default path pointing to the current controller which happens to
be rushing_offenses_path.
I want to call/change this path based on the controller that calls it.
Unfortunately, I searched forever, and watched many rails casts to see
if I could find the information. While I learned a ton of new things
with helpers today, I couldn’t find any information on what I’m trying
to do.
Many thanks in advance.
On Tue, 2009-06-16 at 03:02 +0200, Älphä Blüë wrote:
I want to call/change this path based on the controller that calls it.
Not sure if this is what you seek, but all views have access to the
controller / action that rendered them via controller.controller_name
and controller.action_name.
e.g.;
<% if controller.controller_name == “rushing_offenses” -%>
some stuff
<% end %>
HTH,
Bill
You can use that technique to do something like this in your helper:
link_to(“Link!”, { :action => “blah”, :controller =>
controller.controller_name })
You don’t have to use named routes all the time. There are definitely
times where the path hash is advantageous.
I created the following for my TableHelper which will allow all 37
models to use the same thing:
def sort_column(title, direction)
(direction == “asc” ? image = “up.gif” : image = “down.gif”)
(link_to image_tag(image, :border=>0), :controller =>
controller.controller_name, :numteams => (@showall ? 120 : nil),
:orderby => title, :sortby => direction) if !@searchteams
end