Jquery & Rails 3:從 js 文件運行控制器操作? (Jquery & Rails 3: Run controller action from js file?)


問題描述

Jquery & Rails 3:從 js 文件運行控制器操作? (Jquery & Rails 3: Run controller action from js file?)

I would like to click a div on a page to run a certain number of javascript tasks, but also to run an action on a controller. To explain in greater detail:

I have a untitled.js file in which I have put a bunch of jquery code :

 $(function(){
$('nav li').hover(function(){
        $(this).addClass("bigger", 50);
    }, function(){
        $(this).removeClass("bigger", 50);
});

$('#notify').click(function(){
        $('.notifications').show();
});

$('#notify').click(function(){
        (some code to run a controller action)
});


 });

I would like to run a controller action called "run_tasks" on the "Users" controller when the #notify button is clicked, as well as showing a notifications class. As you can see in the above code I have the jquery code for showing .notifications sorted out, but I don't know what to put in "(some code to run a controller action)". To provide information about the controller action, it will just run some tasks that I want to run, it doesn't redirect to a new page or anything. Here is the basic format of the users controller:

 class UsersController < ApplicationController
      ....
      def run_tasks

      end
 end

Is what I am trying to do possible, and if so what jquery code do I put in there? I'm using Rails 3.0.9 and the newest jquery.

‑‑‑‑‑

參考解法

方法 1:

As long as you have a route for that controller action, you can cause it to run by simply issuing an ajax request to it with jQuery's $.get().

$('#notify').click(function(){
    $('.notifications').show();
    $.get('/users/run_tasks');
});

There's no need to do two $('#notify').click() either, you can put the two statements in one block (as I have done in the code snippet above).

Note that in this case .get() simply makes the request to call the controller action on the server and ignores the result, but you can also use it to handle data returned by the controller. Refer to the doc link above.

(by jayshelhamer)

參考文件

  1. Jquery & Rails 3: Run controller action from js file? (CC BY‑SA 3.0/4.0)

#controller #jquery #ruby-on-rails-3 #ruby-on-rails #routing






相關問題

覆蓋我的控制器,我應該在哪個事件中檢查 cookie? (Overriding my controller, which event should I check for a cookie in?)

ASP.NET MVC,將模型從視圖傳遞到控制器 (ASP.NET MVC, passing Model from View to Controller)

Ruby on Rails:更改列表順序後未顯示錯誤消息 (Ruby on Rails: Error messages not displaying since changing order of a list)

Успадкоўванне кантролера ASP.NET MVC 4 (ASP.NET MVC 4 controller inheritance)

如何在 CakePHP 中創建自定義 MySQL 查詢? (How to create custom MySQL queries in CakePHP?)

Respond_to 沒有調用正確的格式 (Respond_to does not call the right format)

Rails - 尋找錯誤 ID 的 JSON 回調 (Rails - JSON callback looking for wrong id)

YIi PHP - 帶有 foreach 循環的輸出數組 (YIi PHP - Output array with a foreach loop)

使用 SQL 將方法邏輯移動到 Rails 項目中的模型 (Moving method logic with SQL to model in rails project)

MVC 為什麼要單元測試控制器 (MVC Why unit test controllers)

Jquery & Rails 3:從 js 文件運行控制器操作? (Jquery & Rails 3: Run controller action from js file?)

.net core mvc:在區域上添加具有 crud 功能的控制器未正確生成代碼和鏈接 (.net core mvc: adding controller with crud functionality on area didn't generate code and link correctly)







留言討論