简体   繁体   English

Rails link_to 工作怪异

[英]Rails link_to working wierd

I have set up a link_to in a partial (update_dashboard) and it looks like this我在部分(update_dashboard)中设置了一个link_to,它看起来像这样

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. 'hash' 是我传递给控制器​​的一组输入。

As it can be seen.可以看出。 i want the "Link" to map to the 'update_report' action in the 'results' controller.我希望“链接”映射到“结果”控制器中的“update_report”操作。

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我进入了 Firebug,这就是链接的呈现方式

<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 ?为什么 href 指向 /test/update_dashboard ? Why is is not taking the parameter that i supplied for the controller attribute为什么不采用我为控制器属性提供的参数

Any help is greatly appreciated.任何帮助是极大的赞赏。

Try,尝试,

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

and if you are not using rails current version, try using如果您没有使用 rails 当前版本,请尝试使用

link_to_remote "link", :update => "results",
:url => { :controller => "results", :action => "update_report", :hash => hash }

Reference 参考

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看看link_to方法源: 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' 变量是带有键和值的真实哈希,您需要将它与 url 哈希合并,如下所示:

<% 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM