Basically, I'm trying to have a rails method execute when the user clicks on a link (or a button or some type of interactive element).
基本上,我试图在用户点击链接(或按钮或某种类型的交互元素)时执行rails方法。
I tried putting this in the view:
我试着把它放在视图中:
<%= link_to_function 'Add' , function()%>
But that didn't seem to work. It ended up simply calling the function without a user even clicking on the "Add" link.
但这似乎不起作用。它最终只是简单地调用该函数而没有用户甚至点击“添加”链接。
I tried it with link_to as well, but that didn't work either. I'm starting to think there isn't a clean way to do this. Anyway, thanks for your help.
我也尝试使用link_to,但这也无效。我开始认为没有一种干净的方法可以做到这一点。无论如何,谢谢你的帮助。
PS. I defined the method in the ApplicationController and it is a helper method.
PS。我在ApplicationController中定义了该方法,它是一个辅助方法。
1 个解决方案
#1
11
The view and the controller are independent of each other. In order to make a link execute a function call within the controller, you need to do an ajax call to an endpoint in the application. The route should call the ruby method and return a response to the ajax call's callback, which you can then interpret the response.
视图和控制器彼此独立。为了使链接在控制器内执行函数调用,您需要对应用程序中的端点执行ajax调用。该路由应调用ruby方法并返回对ajax调用的回调的响应,然后您可以解释响应。
As an example, in your view:
例如,在您的视图中:
<%= link_to 'Add', '#', :onclick => 'sum_fn()' %>
<%= javascript_tag do %>
function sum_fn() {
/* Assuming you have jQuery */
$.post('/ajax/sum.json', {
num1: 100,
num2: 50
}, function(data) {
var output = data.result;
/* output should be 150 if successful */
});
}
<% end %>
In your routes.rb
add a POST
route for your ajax call endpoint.
在您的routes.rb中为您的ajax调用端点添加POST路由。
post '/ajax/sum' => 'MyController#ajax_sum'
Then suppose you have a MyController class in mycontroller.rb
. Then define the method ajax_sum
.
然后假设你在mycontroller.rb中有一个MyController类。然后定义方法ajax_sum。
class MyController < ApplicationController
# POST /ajax/sum
def ajax_sum
num1 = params["num1"].to_i
num2 = params["num2"].to_i
# Do something with input parameter and respond as JSON with the output
result = num1 + num2
respond_to do |format|
format.json {render :json => {:result => result}}
end
end
end
Hope that hopes!
希望希望!
#1
11
The view and the controller are independent of each other. In order to make a link execute a function call within the controller, you need to do an ajax call to an endpoint in the application. The route should call the ruby method and return a response to the ajax call's callback, which you can then interpret the response.
视图和控制器彼此独立。为了使链接在控制器内执行函数调用,您需要对应用程序中的端点执行ajax调用。该路由应调用ruby方法并返回对ajax调用的回调的响应,然后您可以解释响应。
As an example, in your view:
例如,在您的视图中:
<%= link_to 'Add', '#', :onclick => 'sum_fn()' %>
<%= javascript_tag do %>
function sum_fn() {
/* Assuming you have jQuery */
$.post('/ajax/sum.json', {
num1: 100,
num2: 50
}, function(data) {
var output = data.result;
/* output should be 150 if successful */
});
}
<% end %>
In your routes.rb
add a POST
route for your ajax call endpoint.
在您的routes.rb中为您的ajax调用端点添加POST路由。
post '/ajax/sum' => 'MyController#ajax_sum'
Then suppose you have a MyController class in mycontroller.rb
. Then define the method ajax_sum
.
然后假设你在mycontroller.rb中有一个MyController类。然后定义方法ajax_sum。
class MyController < ApplicationController
# POST /ajax/sum
def ajax_sum
num1 = params["num1"].to_i
num2 = params["num2"].to_i
# Do something with input parameter and respond as JSON with the output
result = num1 + num2
respond_to do |format|
format.json {render :json => {:result => result}}
end
end
end
Hope that hopes!
希望希望!