我有这个JQuery代码:
$("p.exclamation,div#notification_Box").live("mouSEOver",function() { });
我想从jQuery代码里面把这个rails方法称为回调函数:
def render_read self.user_notifications.where(:read => false).each do |n| n.read = true n.save end end
解决方法
进行AJAX调用,设置路由,使用控制器操作进行响应并调用方法。
# whatever.js $("p.exclamation,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:此代码适用于Rails 3.x和Ruby 1.9.x