1

在我的索引视图中,我希望添加一个按钮来执行一个名为myFunction(myArg). 该功能位于behaviours/myRubyFile.rb. 如何创建一个使用特定参数执行该功能的按钮。

编辑:现在我收到一个新错误

The action 'my_func' could not be found for FunctionsController

但是my_func在 FunctionsController 中定义了一个函数。

4

1 回答 1

2

通常,您希望从该按钮向服务器发送请求,因此服务器运行 myFunction。要实现这一点,请按照下列步骤操作:

1 - 确保 behavior/myRubyFile.rb 定义了一个模块:

module MyRubyModule
  def myFunction(arg)
    #Your logic here
  end
end

2 - 在 routes.rb 中定义一个路由:

match '/my_func' => "functions#my_func"

3 - 定义函数控制器:

class FunctionsController < ApplicationController
  include MyRubyModule

  def my_func
    arg = params[:arg]
    myFunction(arg)
  end
end

4 - 在视图中创建一个按钮以将请求发送到服务器:

<%= button_to "Button", {:action => "my_func", :controller => "functions", :arg => "YOUR ARG HERE"} %>

我没有测试过这段代码,但它应该可以解决问题。

编辑:添加:arg => "YOUR ARG HERE"到路线(Thnx nzifnab)

于 2012-09-11T18:30:42.360 回答