How to call a rails method from in jQuery

前端 未结 4 1907
梦如初夏
梦如初夏 2020-12-04 16:03

I have this JQuery code:

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

    });

and I want to call this rails

4条回答
  •  庸人自扰
    2020-12-04 16:45

    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 PUT as the http method.

    Here's an example route:

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

    Go ahead and add the render_read action to your controller.

    Your jQuery code will look something like this:

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

提交回复
热议问题