繁体   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?

我想知道在当前的rails3 master中是否完全破坏了基本的link_to语法,或者我是否在这里做了一些错误的语法。

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

实际上应该在点击时产生警报。 很简单。 不适用于我的rails3项目! (也没有错误输出!)任何想法?

对于一般的link_to语法,我找不到一个例子,我可以将link_to_remote与确认,远程和html类结合起来(参见下面的尝试)

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

甚至rails3 api对我没有帮助: http//rails3api.s3.amazonaws.com/index.html

救命!

我相信你的问题在于你设置链接以在被舔时显示警报,而不是点击时。 ;)

至于link_to_remote ,它已经改变为切换到不显眼的javascript。 你可以在这里阅读: http//blog.solnic.eu/2009/09/08/unobtrusive-javascript-helpers-in-rails-3.html

好吧,看起来新的不引人注目的javascript更改引入了问题。 如果您遇到类似问题,请参阅以下帖子以获取更多信息http://blog.loopedstrange.com/modest-rubyist-archive/rails-3-ujs-and-csrf-meta-tags

 <%= csrf_meta_tag %>

为我固定的东西。

零不起作用:

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

你应该使用:

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

如果这里没有其他答案对你有用,那么也许这会有所帮助。

所以csrf_meta_tag声明对我来说还不够,但无论如何都应该在你的Rails 3的布局文件中添加。 有了我,结果证明是与jQuery的冲突。 我只想说:

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

在我的布局中的rails脚本标记之后,Prototype和jQuery之间的冲突得到了解决。 嘿presto我在删除时收到确认对话框。

当使用link_to尝试删除记录时,此技术也解决了我的原始问题。 使用link_to任何destroy命令似乎都会重定向到记录的show页面。 因此我根据我看到的其他解决方案移动到button_to,但没有确认。 我想知道jQuery和Prototype是否存在一些更深层次的问题。

这一切都发生在升级的Rails 2.3.5应用程序上,似乎工作正常,无需在我的布局文件中包含Prototype或:defaults。

在旁注上我确实按照这些说明操作:

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

尝试为这个项目一起丢失Prototype,并使用jQuery git子模块代替Rails 3。 按照这些说明不起作用,当使用link_to时,我仍然没有使用button_to和显示页面确认对话框。 只是想我会提到它,所以省去了尝试这个的麻烦。

由于我无法发表评论,因此上述答案的偏离主题评论:(

@robeastham:我想我会留下一些评论,可能有助于解决你遇到的一些问题。

自从从ActiveRecord切换到Mongoid以来,我一直在将“销毁链接重定向到显示页面而不是索引页面”,并且没有找到真正的解决方案(已经删除了原型)。 但是,干净利落的解决方法是使用以下命令明确指定重定向路径:location to respond_with:

respond_with @themodel, :location => themodels_url

至于使用按钮获取确认弹出窗口,您可以执行以下操作:

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

例如

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

这对我有用:

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

检查它是否在views \\ layout \\ application.html.erb(或等效文件)中:

<%= csrf_meta_tags %>

请注意,Rails v3.2.2使用“标签”而不是“标签”

暂无
暂无

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

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