How to call a rails method from in jQuery

前端 未结 4 1892
梦如初夏
梦如初夏 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:35

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

    # 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

提交回复
热议问题