Search code examples
ruby-on-railslink-to

Rails link_to working wierd


I have set up a link_to in a partial (update_dashboard) and it looks like this

link_to "Link", hash, {:url => {:controller => "results", :action => "update_report"}},     :remote => true

'hash' is a set of inputs that I am passing to the controller.

As it can be seen. i want the "Link" to map to the 'update_report' action in the 'results' controller.

but, i find that after the page is rendered, when I click on the link, it just displays the partial in a new page.

I went into Firebug and this is how the link is rendered

<a url="{:controller=>"results", :action=>"update_report"}" href="/test/update_dashboard?branch=xxxx&brand=xx&end_time=2012-02-29+22%3A59&repo=xxxx%2Fxx&start_time=2012-02-17+18%3A20">Link</a>

Why is the href pointing to /test/update_dashboard ? Why is is not taking the parameter that i supplied for the controller attribute

Any help is greatly appreciated.


Solution

  • Take a look at link_to method source at: https://github.com/rails/rails/blob/master/actionpack/lib/action_view/helpers/url_helper.rb#L236

    Since 'hash' variable is a real hash with keys and values you need to merge it with url hash like this:

    <% hash = { :param1 => "value1", :param2 => "value2"}  %>
    <%= link_to 'MyLink', { :controller => "questions", :action => "index" }.merge(hash), :id => "link_id", :class => "link_class" %>
    

    It produces the link you want:

    <a href="/questions?param1=value1&amp;param2=value2" class="link_class" id="link_id">MyLink</a>