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 %>
You can pass more attributes in the path helpers:
From the documentation seen on APIdock.com #link_to: