I'm trying to add paging parameters to link_to inside an application_helper method.
def index_show_link(elm, link_text = 'Show')
(can?(:read, elm)) ? link_to( link_text, elm ) : ' '.html_safe
end
I want to add the :page => params[:page] pair into the link, but I can't seem to find a way to add it. If I use the long-hand syntax: {:id => elm.id, :controller => controller_name, :action => :show, :page => params[:page]} then it works, but that also horses around with friendly_id.
I've looked through the link_to and url_for documentation, and I can't see a way to pass the bare object to either method and also include the extra parameter.
What's the trick?
Thanks,
Walter
I found that substituting to_param for id makes FriendlyId work correctly again, and this one is solved. Still seems awfully long-hand.
Walter
> I'm trying to add paging parameters to link_to inside an application_helper method.
> def index_show_link(elm, link_text = 'Show')
> (can?(:read, elm)) ? link_to( link_text, elm ) : ' '.html_safe
> end
> I want to add the :page => params[:page] pair into the link, but I can't seem to find a way to add it. If I use the long-hand syntax: {:id => elm.id, :controller => controller_name, :action => :show, :page => params[:page]} then it works, but that also horses around with friendly_id.
I found that substituting to_param for id makes FriendlyId work correctly again, and this one is solved. Still seems awfully long-hand.
link_to 'text', blah
is a more convenient way of writing link_to 'text',
polymorphic_path(blah) or link_to 'text', blah_path(blah) (assuming
you have resources :blahs).
Both of those two _path helpers should allow you to specify extra
options (i.e. polymorphic_path(blah, :page => 2))
Fred
I'm trying to add paging parameters to link_to inside an application_helper method.
def index_show_link(elm, link_text = 'Show')
(can?(:read, elm)) ? link_to( link_text, elm ) : ' '.html_safe
end
I want to add the :page => params[:page] pair into the link, but I can't seem to find a way to add it. If I use the long-hand syntax: {:id => elm.id, :controller => controller_name, :action => :show, :page => params[:page]} then it works, but that also horses around with friendly_id.
I found that substituting to_param for id makes FriendlyId work correctly again, and this one is solved. Still seems awfully long-hand.
link_to 'text', blah
is a more convenient way of writing link_to 'text',
polymorphic_path(blah) or link_to 'text', blah_path(blah) (assuming
you have resources :blahs).
Both of those two _path helpers should allow you to specify extra
options (i.e. polymorphic_path(blah, :page => 2))
So are you saying that if I'm in the application helper, I can use a polymorphic_path, even though I am not declaring anything to be polymorphic? That's a new one on me.
Walter
I'm trying to add paging parameters to link_to inside an application_helper method.
def index_show_link(elm, link_text = 'Show')
(can?(:read, elm)) ? link_to( link_text, elm ) : ' '.html_safe
end
I want to add the :page => params[:page] pair into the link, but I can't seem to find a way to add it. If I use the long-hand syntax: {:id => elm.id, :controller => controller_name, :action => :show, :page => params[:page]} then it works, but that also horses around with friendly_id.
I found that substituting to_param for id makes FriendlyId work correctly again, and this one is solved. Still seems awfully long-hand.
link_to 'text', blah
is a more convenient way of writing link_to 'text',
polymorphic_path(blah) or link_to 'text', blah_path(blah) (assuming
you have resources :blahs).
Both of those two _path helpers should allow you to specify extra
options (i.e. polymorphic_path(blah, :page => 2))
So are you saying that if I'm in the application helper, I can use a polymorphic_path, even though I am not declaring anything to be polymorphic? That's a new one on me.
Refactored, works great! Thanks!
Walter
polymorphic_path is a rails provided method - it's what gets called by
rails when you shorthand stuff like form_for(some_object) or link_to
'blah', some_other_object. It basically just looks at the class name
(probably via the activemodel::naming) stuff and then calls the
explicit path helper (comment_path etc) that it expects to exist given
what you're asking it to do.
Fred