Search code examples
ruby-on-railsruby-on-rails-3link-to

Pass additional parameters with link_to


I have a news page containing several news with pagination. I want to allow the user to view the new's details and when he clicks on a "back" button, the view goes to the page the user was previously on. My idea was to add a parameter to the link_to which brings the user on the show page, but this doesn't seem to work:

<%= link_to news_path(new.id, :back_page => params[:page]) do %>
    <span class="glyphicon glyphicon-eye-open"></span>
<% end %>

I would want the url rendered to be something like this: news/2?back_page=2. But I always get news/2.

How can I do this?

Thanks.

Edit

Here's how news_path looks like in my routes: http://pastebin.com/EAhDQyf4


Solution

  • I resolved my problem. Make sure the value you give to your additional parameters is something. When I go to the news index page, even though the selected page is the first one, there is no parameter page=1. The parameter only appears once click on a different page (or you go to another page and come back to the first one). If you give an additional parameter to the link_to and its value is nothing, the rendered link will be (in my case):

    <a href="/news/2" />
    

    Instead of:

    <a href="/news/2?back_page=" />
    

    So you might get trapped, just like I was.