简体   繁体   English

jQuery ajax在Rails中调用吗?

[英]jQuery ajax call in Rails?

I want to execute a simple thing. 我想执行一件简单的事情。 When the user clicks the link jQuery needs to generate an Ajax request which sends the id of the link item to a method in the controller. 当用户单击链接时,jQuery需要生成一个Ajax请求,该请求会将链接项的ID发送到控制器中的方法。 Basically I want a nice looking modal dialog window when the user clicks the delete link. 基本上,当用户单击删除链接时,我想要一个漂亮的模态对话框窗口。 That window needs to contain various info about the deleted items. 该窗口需要包含有关已删除项目的各种信息。 Eg <%= link_to "Delete", item, :id => "delete" %> 例如<%= link_to "Delete", item, :id => "delete" %>

I'm able to select this link with jquery and even to open a popup dialog when the user clicks it. 我可以使用jquery选择此链接,甚至在用户单击它时也可以打开一个弹出对话框。 Through application.js but what i really need is jQuery to call my method when the link is clicked and in my method I would answer via format.js. 通过application.js,但我真正需要的是jQuery在单击链接时调用我的方法,而在我的方法中,我将通过format.js进行回答。 In the js file I place the code to show modal dialog with required parameters. 在js文件中,我放置代码以显示具有所需参数的模态对话框。 All those actions asynchonous via ajax ofcourse. 所有这些动作都是通过课程的异步进行的。 I can't generate the ajax request to the method in the controller. 我无法向控制器中的方法生成ajax请求。 Don't know already how to deal with the jQuery.ajax method, especially how to force jQuery to pass the url parameter to the rails method. 还不知道如何处理jQuery.ajax方法,尤其是如何强制jQuery将url参数传递给rails方法。 I watched the railscast and studied the example with .post, but I don't need to submit any form. 我观看了railscast并使用.post研究了该示例,但是我不需要提交任何表单。 Any advise is appreciated. 任何建议表示赞赏。 Thanks 谢谢

Rails expects a post, with a hidden param. Rails希望发布一个带有隐藏参数的帖子。

$('#delete').click(function(e){
  e.preventDefault();
  $.post($(this).attr('href'), { _method: 'delete' });
});

Rails 3 has an unobtrusive way of doing this using a jQuery plugin: Rails 3使用jQuery插件可以做到这一点:

http://github.com/rails/jquery-ujs

Once you have that loaded into your layout (with jQuery 1.4.1 or newer), you can do interactions like this pretty easily. 一旦将其加载到布局中(使用jQuery 1.4.1或更高版本),就可以非常轻松地进行这样的交互。

<%= link_to 'delete', item, :remote => true, :method => :delete, 
       :confirm => 'Are you sure?' %>

In Rails 3 all this does is add some HTML attributes to your link. 在Rails 3中,所有要做的就是在链接中添加一些HTML属性。 These get caught by live handlers in the jQuery plugin. 这些被jQuery插件中的实时处理程序捕获。 It's easy to use this behavior in a Rails 2 application, though: 不过,在Rails 2应用程序中使用此行为很容易:

<%= link_to 'delete', item, :'data-remote' => true, :'data-method' => :delete, 
       :'data-confirm' => 'Are you sure?' %>

Note that you need to have the jQuery plugin loaded in your view. 请注意,您需要在视图中加载jQuery插件。

http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

If you need the initial response to be a modal instead of an alert, you could make the first link a bit simpler: 如果您需要将初始响应作为模式而不是警报,则可以使第一个链接更简单:

<%= link_to 'delete', modal_confirm_item_url(item), :'data-remote' => true %>

You could link it to some pre-existing member action. 您可以将其链接到一些预先存在的成员操作。 Or since you may eventually want to use .js responses from those actions to serve other ajax requests, it may make sense to create a new action to handle this behavior. 或者,由于您最终可能希望使用这些操作中的.js响应来满足其他Ajax请求,因此创建一个新操作来处理此行为可能很有意义。

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

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