I want to have a button that, when clicked, will execute rails code. Is javascript necessary for this? Is there some ruby-javascript (.js.rb)?
if button is clicked… execute A
However i do not want to redirect or go to a new view, just ruby code to be executed.
Yes. Try googling unobtrusive javascript. With rails specifically the following is the general principle (rails v 3.0 or greater), and is most often used for forms.
Here is an example of using a remote form to post a “message” via javascript.
First setup your controller to respond to javascript. So for example a “create” action would look something like this:
Now create a file called “create.js.erb”. This follows the rails naming conventions for controller actions, but note the js.erb extension.
In this code you can put whatever javascript you want to respond with, and in rails 3.1 or you can use jquery by default as well.
Then in your view that you want to initiate this javascript call you would put a form like this:
This is a standard form_for. Only thing special here to notice is the :remote => true parameter at the beginning.
Anyways hope this helps 🙂