带有rails3(link_to_remote)的link_to语法和基本javascript在rails3应用程序中不起作用?

发布于 2024-08-25 13:56:48 字数 666 浏览 13 评论 0原文

我想知道当前的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

帮助!

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.

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

should actually produce an alert on click. very simple. does not work on my rails3 project! (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 "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

help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

最舍不得你 2024-09-01 13:56:48

我相信您的问题是您已将链接设置为在单击时显示警报,而不是在单击时显示警报。 ;)

至于 link_to_remote,它随着切换到不显眼的 javascript 而发生了变化。您可以在这里阅读:http ://blog.solnic.eu/2009/09/08/unobtrusive-javascript-helpers-in-rails-3.html

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. You can read about it here: http://blog.solnic.eu/2009/09/08/unobtrusive-javascript-helpers-in-rails-3.html

苏辞 2024-09-01 13:56:48

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

 <%= csrf_meta_tag %>

为我解决了问题。

ok it looks like the new unobtrusive javascript changes introduced the problem.
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

 <%= csrf_meta_tag %>

fixed things for me.

风和你 2024-09-01 13:56:48

nil 不起作用:

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

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>
无风消散 2024-09-01 13:56:48

如果这里的其他答案都不适合您,那么也许这会有所帮助。

所以 csrf_meta_tag 声明对我来说还不够,但无论如何应该添加到 Rails 3 的布局文件中。对我来说,这是与 jQuery 的冲突。我只是把这个:

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

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

这项技术还解决了我在使用 link_to 尝试删除记录时遇到的原始问题。使用 link_to 任何 destroy 命令似乎都会重定向到显示页面以进行记录。因此,我根据我看到的其他一些解决方案转移到了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 和显示页面的确认对话框。只是想我会提到它,这样就可以省去别人尝试这个的麻烦。

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. With me it turned out to be a conflict with 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. Hey presto I was getting the confirmation dialog box on delete.

This technique also resolved my original issue when using link_to to try and delete a record. With link_to any destroy command seemed to redirecting to the show page for the record. Hence I moved to button_to based on some other solution I saw, but with no confirmation. I wonder if there are some more deep seated issues with jQuery and 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.

On a side note I did follow these instructions:

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. Following these instructions did not work and I was still without confirm dialogs with button_to and the show page when using link_to. Just thought I'd mention it so save someone the trouble of trying this.

情绪 2024-09-01 13:56:48

对上面答案的题外话评论,因为我还不能发表评论:(

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

自从从 ActiveRecord 切换到 Mongoid 以来,我一直遇到“销毁链接重定向到显示页面而不是索引页面”问题,并且还没有找到真正的解决方案(已经删除了原型)。但是,一种干净的解决方法是使用 :location 明确指定重定向路径以 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 }

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.

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). However, a workaround that works cleanly is to explicitely specify the re-direct path using :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> }

e.g.

button_to "Click Here", { :controller => "home", :action -> :set_completed, :confirm => "Mark the item complete?" }, { :method => :put }
舞袖。长 2024-09-01 13:56:48

这对我有用:

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

检查这是否在您的views\layout\application.html.erb(或同等内容)中:

<%= csrf_meta_tags %>

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

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):

<%= csrf_meta_tags %>

Note that Rails v3.2.2 uses "tags" not "tag"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文