← Back to Upcase

link_to with optional params

(jon) #1

And I’m stumped. I’m trying to link to the previous page (if available) and the next page (if available). The links show up in the right circumstances, but I cannot get them to link to the proper URL.

I’ve tried a number of methods to this approach:

<%= link_to 'Prev', url_for(params), class: 'ladder-nav-button pull-left' if has_prev_page? %>
<%= link_to 'Next', ladders_path(params), class: 'ladder-nav-button pull-right' if has_next_page? %>

In addition to things like hardcoding the values:

<%= link_to 'Prev', url_for(region: 'all', page: 1), class: 'ladder-nav-button pull-left' if has_prev_page? %>

I always end up with a link to /ladders, and not /ladders/all/1 or something like that.

The params are there. I can print them out on the page, but the links fail. Here is the relevant route (from rake routes):

ladders GET /ladders(/:region(/:page))(.:format) ladders#show {:page=>1, :region=>"all"}

The region and page are optional, but default to ‘all’ and 1 if not specified.

Where have I tripped myself up at? Is there some “gotcha” with optional params?

0 Likes