如何在 jQuery 中调用 rails 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5933261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:02:57  来源:igfitidea点击:

How to call a rails method from in jQuery

jqueryruby-on-railsrubyruby-on-rails-3callback

提问by user730569

I have this JQuery code:

我有这个 JQuery 代码:

$("p.exclamation, div#notification_box").live("mouseover", function() {

    });

and I want to call this rails method from inside the jQuery code as a callback:

我想从 jQuery 代码内部调用这个 rails 方法作为回调:

def render_read
  self.user_notifications.where(:read => false).each do |n|
    n.read = true
    n.save
  end
end

This method is in my user model. Is there any way to do this?

这个方法在我的用户模型中。有没有办法做到这一点?

回答by edgerunner

Make an AJAX call, set up a route, respond with a controller action and call your method.

进行 AJAX 调用,设置路由,使用控制器操作进行响应并调用您的方法。

# whatever.js
$("p.exclamation, div#notification_box").on("mouseover", function() {
  $.ajax("/users/render_read")
});

# routes.rb
resources :users do
  get :render_read, on: :collection 
  # or you may prefer to call this route on: :member
end

# users_controller.rb
def render_read
  @current_user.render_read
  # I made this part up, do whatever necessary to find the needed user here
end

PS: This code is for Rails 3.x and Ruby 1.9.x

PS:此代码适用于 Rails 3.x 和 Ruby 1.9.x

回答by Andy Gaskell

It's good that you have that model code. We'll need to add a new action and make sure your route is setup. If you're using resources you'll have to add collection or member. Since you're doing an update I would choose PUTas the http method.

很高兴您拥有该模型代码。我们需要添加一个新操作并确保您的路线已设置。如果您正在使用资源,则必须添加集合或成员。由于您正在进行更新,我会选择PUThttp 方法。

Here's an example route:

这是一个示例路线:

resources :user_notifications do
??collection do
????put 'render_read'
??end
end

Go ahead and add the render_readaction to your controller.

继续并将操作添加render_read到您的控制器。

Your jQuery code will look something like this:

您的 jQuery 代码将如下所示:

$("p.exclamation, div#notification_box").live("mouseover", function() {   
  $.ajax({
    url: "/user_notifications/render_read",
    type: 'PUT'
  });
});

回答by Javier Giovannini

Normally JavaScript works in the client side, but it's is also possible that your application draws a javaScript function for a each client. In that case you can use the <%=and tags %>in a .erb file:

通常 JavaScript 在客户端工作,但您的应用程序也可能为每个客户端绘制一个 javaScript 函数。在这种情况下,您可以在 .erb 文件中使用<%=和 标签%>

<script type="text/javascript">
$(function(){
    new AClass.function({
        text: <%= Date.today %>
    });
});
</script>

回答by mu is too short

You would need to set up a controller on the server side to call your render_readmethod and then you could use $.ajaxor $.postin your jQuery to make the request.

您需要在服务器端设置一个控制器来调用您的render_read方法,然后您可以使用$.ajax$.post在您的 jQuery 中发出请求。