Reputation: 3709
In my index view I am looking to add a button that will execute a function called myFunction(myArg)
. This function is located in behaviours/myRubyFile.rb
. How can I create a button that executes that function with a particular argument.
EDIT: Now I am getting a new error
The action 'my_func' could not be found for FunctionsController
However there is a function my_func
defined in FunctionsController.
Upvotes: 1
Views: 156
Reputation: 15788
Generally, you'd want to send a request to the server from that button, so the server runs myFunction. To achieve this follow these steps:
1 - make sure behaviours/myRubyFile.rb defines a module:
module MyRubyModule
def myFunction(arg)
#Your logic here
end
end
2 - define a route in routes.rb:
match '/my_func' => "functions#my_func"
3 - define FunctionsController:
class FunctionsController < ApplicationController
include MyRubyModule
def my_func
arg = params[:arg]
myFunction(arg)
end
end
4 - create a button in the view to send the request to the server:
<%= button_to "Button", {:action => "my_func", :controller => "functions", :arg => "YOUR ARG HERE"} %>
I haven't tested this code, but it should do the trick.
EDIT: added :arg => "YOUR ARG HERE"
to the route (Thnx nzifnab)
Upvotes: 2