Rails 3.1 link_to remote => true呈现HTML而不是JS,在某些情况下,相同的代码也可以工作 [英] Rails 3.1 link_to remote => true renders HTML instead of JS, the same code works in some cases

查看:89
本文介绍了Rails 3.1 link_to remote => true呈现HTML而不是JS,在某些情况下,相同的代码也可以工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在Rails 3.1中拥有的代码

This is the code I have in rails 3.1

<%= link_to 'All',:action => "bycategory", :id => 'All', :remote => true %>

我已经定义了一个js.erb.这段代码工作得很好,即当与特定CSS放在表中时调用JS.当我只是更改表ID或放入无序列表或其他地方时,它在查找html.erb时给了我模板错误.击败我:(.

I have defined a js.erb. This code works perfectly fine i.e invokes the JS when put in table with a certain CSS. When I just change the table id or put in a unordered list or anywhere else, it gives me template error as it looks for the html.erb. Just beats me :(.

我发现的一件事是,在第一种情况下的Params中,除了id和remote之外,它还发送时间戳 在第二种情况下,缺少时间戳.不知道为什么会这样 任何帮助将不胜感激,花了太多时间调试此

one thing I found is that in the Params in the first case apart from the id and the remote , it also sends timestamp In the second case time stamp is missing. Not sure why this is happening Any help will be appreciated, have spent too much time debugging this

推荐答案

您可能希望将:action =>"bycategory"和:id =>'All'放入哈希中,

You might want to put the :action => "bycategory" and :id => 'All' in a hash as

<%= link_to 'All', { :action => "bycategory", :id => 'All' }, :remote => true %>

这将生成适当的html属性,即data-remote ="true",否则它将仅将后面的所有内容都视为http请求属性.您的代码将生成-

This will generate the proper html attribute i.e. data-remote="true" otherwise it will simply treat everything following as an http request attribute. Your code will generate -

<a href="/bycategory?id=All&amp;remote=true">All</a>

而使用散列将生成类似于

whereas using the hash will generate something similar to

<a data-remote="true" href="/bycategory/All">All</a>

这篇关于Rails 3.1 link_to remote =&gt; true呈现HTML而不是JS,在某些情况下,相同的代码也可以工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆