简体   繁体   English

link_to语法与rails3(link_to_remote)和基本的javascript无法在rails3应用程序中工作?

[英]link_to syntax with rails3 (link_to_remote) and basic javascript not working in a rails3 app?

i am wondering if the basic link_to syntax is completely broken in current rails3 master or if i am doing some wrong syntax here. 我想知道在当前的rails3 master中是否完全破坏了基本的link_to语法,或者我是否在这里做了一些错误的语法。

= link_to "name", nil, :onlick => "alert('Hello world!');"

should actually produce an alert on click. 实际上应该在点击时产生警报。 very simple. 很简单。 does not work on my rails3 project! 不适用于我的rails3项目! (also no error output!) any ideas? (也没有错误输出!)任何想法?

for the general link_to syntax i could not find an example where i could combine a link_to_remote with a confirmation, remote and html class (see my try below) 对于一般的link_to语法,我找不到一个例子,我可以将link_to_remote与确认,远程和html类结合起来(参见下面的尝试)

= link_to "delete", {:action => "destroy", :remote => true, :method => :delete, :confirm => "#{a.title} wirklich Löschen?" }, :class => "trash"

even the rails3 api does not help me here: http://rails3api.s3.amazonaws.com/index.html 甚至rails3 api对我没有帮助: http//rails3api.s3.amazonaws.com/index.html

help! 救命!

I believe your problem here is that you've set the link up to show the alert when it is licked, as opposed to when it is clicked. 我相信你的问题在于你设置链接以在被舔时显示警报,而不是点击时。 ;) ;)

As for link_to_remote , it has changed with the switch to unobtrusive javascript. 至于link_to_remote ,它已经改变为切换到不显眼的javascript。 You can read about it here: http://blog.solnic.eu/2009/09/08/unobtrusive-javascript-helpers-in-rails-3.html 你可以在这里阅读: http//blog.solnic.eu/2009/09/08/unobtrusive-javascript-helpers-in-rails-3.html

ok it looks like the new unobtrusive javascript changes introduced the problem. 好吧,看起来新的不引人注目的javascript更改引入了问题。 see the following post for more information if you run into similar issues http://blog.loopedstrange.com/modest-rubyist-archive/rails-3-ujs-and-csrf-meta-tags 如果您遇到类似问题,请参阅以下帖子以获取更多信息http://blog.loopedstrange.com/modest-rubyist-archive/rails-3-ujs-and-csrf-meta-tags

 <%= csrf_meta_tag %>

fixed things for me. 为我固定的东西。

nil doesn't work: 零不起作用:

= link_to "name", nil, :onclick => "alert('Hello world!');"
=> <a href="/currentpath", onclick="alert('Hello world!');">name</a>

You should use: 你应该使用:

= link_to "name", "#", :onclick => "alert('Hello world!');"
=> <a href="#", onclick="alert('Hello world!');">name</a>

If none of the other answers here work for you then maybe this will help. 如果这里没有其他答案对你有用,那么也许这会有所帮助。

So the csrf_meta_tag declaration was not enough for me, though should be added in your layout file for Rails 3 anyway. 所以csrf_meta_tag声明对我来说还不够,但无论如何都应该在你的Rails 3的布局文件中添加。 With me it turned out to be a conflict with jQuery. 有了我,结果证明是与jQuery的冲突。 I just put this: 我只想说:

<script type="text/javascript">
  jQuery.noConflict();
</script>

after the rails scripts tag in my layout and the clash between Prototype and jQuery was resolved. 在我的布局中的rails脚本标记之后,Prototype和jQuery之间的冲突得到了解决。 Hey presto I was getting the confirmation dialog box on delete. 嘿presto我在删除时收到确认对话框。

This technique also resolved my original issue when using link_to to try and delete a record. 当使用link_to尝试删除记录时,此技术也解决了我的原始问题。 With link_to any destroy command seemed to redirecting to the show page for the record. 使用link_to任何destroy命令似乎都会重定向到记录的show页面。 Hence I moved to button_to based on some other solution I saw, but with no confirmation. 因此我根据我看到的其他解决方案移动到button_to,但没有确认。 I wonder if there are some more deep seated issues with jQuery and Prototype. 我想知道jQuery和Prototype是否存在一些更深层次的问题。

This all happened on an upgraded Rails 2.3.5 app that seemed to be working okay without needing to include Prototype or :defaults in my layout file. 这一切都发生在升级的Rails 2.3.5应用程序上,似乎工作正常,无需在我的布局文件中包含Prototype或:defaults。

On a side note I did follow these instructions: 在旁注上我确实按照这些说明操作:

http://webtech.union.rpi.edu/blog/2010/02/21/jquery-and-rails-3/ http://webtech.union.rpi.edu/blog/2010/02/21/jquery-and-rails-3/

to try and lose Prototype all together for this project and use the jQuery git submodule for Rails 3 instead. 尝试为这个项目一起丢失Prototype,并使用jQuery git子模块代替Rails 3。 Following these instructions did not work and I was still without confirm dialogs with button_to and the show page when using link_to. 按照这些说明不起作用,当使用link_to时,我仍然没有使用button_to和显示页面确认对话框。 Just thought I'd mention it so save someone the trouble of trying this. 只是想我会提到它,所以省去了尝试这个的麻烦。

An off-topic comment for an answer above since I can't comment yet :( 由于我无法发表评论,因此上述答案的偏离主题评论:(

@robeastham: I figured I'd leave some comments that may help with some of the issues you've encoutered. @robeastham:我想我会留下一些评论,可能有助于解决你遇到的一些问题。

I've been having the "destroy link re-directing to the show page instead of the index page" issue as well since switching from ActiveRecord to Mongoid, and haven't found a real solution (already have prototype removed). 自从从ActiveRecord切换到Mongoid以来,我一直在将“销毁链接重定向到显示页面而不是索引页面”,并且没有找到真正的解决方案(已经删除了原型)。 However, a workaround that works cleanly is to explicitely specify the re-direct path using :location to respond_with: 但是,干净利落的解决方法是使用以下命令明确指定重定向路径:location to respond_with:

respond_with @themodel, :location => themodels_url

As for getting a confirmation pop-up with a button, you could do: 至于使用按钮获取确认弹出窗口,您可以执行以下操作:

button_to "Button Name", { :controller => "your/controller", :action => :action_name, :confirm => "Text for the pop-up"}, { :method => :<method name> }

eg 例如

button_to "Click Here", { :controller => "home", :action -> :set_completed, :confirm => "Mark the item complete?" }, { :method => :put }

This worked for me: 这对我有用:

<%= link_to "Recommend", recommend_user_path(@user), :remote => true %>

Check that this is in your views\\layout\\application.html.erb (or equivalent): 检查它是否在views \\ layout \\ application.html.erb(或等效文件)中:

<%= csrf_meta_tags %>

Note that Rails v3.2.2 uses "tags" not "tag" 请注意,Rails v3.2.2使用“标签”而不是“标签”

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

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