Gandalf StormCrow
Gandalf StormCrow

Reputation: 26192

Pagination in ruby-on-rails

I've recently started learning ruby-on-rails and now I've got to the point where I can create something but still not very comfortable with syntax.

Today I've been working on pagination, so as you can see I want to display 15 items per page, so this is working great for now:

<% (0..@items_count).step(15) do |i| %>
     <li class="<%='active' if params[:offset].to_i == i%>"><%= link_to i/15+1, items_path(:offset => i) %></li>
<% end %>

Here is the part where I'm stuck :

<% (0..@items_count).step(15) do |i| %>
     <li class="<%='active' if params[:offset].to_i == i%>"><%= link_to i/15+1, items_path(:offset => i, :age => 10) %></li>
<% end %>

I'm trying to pass some additional params beside offset but not able to do it because of the language skill limitation (from my side)

So currently my link looks like this :

http://localhost:3000/items?offset=20

But my point is to have links with multiple params like this :

http://localhost:3000/items?offset=20&age=10

But not just one but n params

Solution :

thanks to MrYoshiji for providing the answer. I was trying to do it directly with :age => "params[:age]"

But this worked :

:age=> "#{params[:age]}"

Logic applied to the example above :

<% (0..@items_count).step(15) do |i| %>
         <li class="<%='active' if params[:offset].to_i == i%>"><%= link_to i/15+1, items_path(:offset => i, :age => "#{params[:age]}") %></li>
<% end %>

Upvotes: 0

Views: 650

Answers (1)

MrYoshiji
MrYoshiji

Reputation: 54882

You can pass more attributes in the path helpers:

items_path(:offset => i, :age => 10) 
# should generates an url with get params like following: 
/items?offset=12&age=10 # (assuming 'i' = 12)

From the documentation seen on APIdock.com #link_to:

link_to "Nonsense search", searches_path(:foo => "bar", :baz => "quux")
# => <a href="/searches?foo=bar&baz=quux">Nonsense search</a>

Upvotes: 2

Related Questions