use*_*569 20 ruby jquery ruby-on-rails callback ruby-on-rails-3
我有这个JQuery代码:
$("p.exclamation, div#notification_box").live("mouseover", function() {
});
Run Code Online (Sandbox Code Playgroud)
我想从jQuery代码中调用这个rails方法作为回调:
def render_read
self.user_notifications.where(:read => false).each do |n|
n.read = true
n.save
end
end
Run Code Online (Sandbox Code Playgroud)
此方法在我的用户模型中.有没有办法做到这一点?
edg*_*ner 35
进行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
Run Code Online (Sandbox Code Playgroud)
PS:此代码适用于Rails 3.x和Ruby 1.9.x.