So I feel really stupid right now, but I can't seem to find an answer.
So I have a method which needs to be called EXACTLY once, and since this is only the experimental phase, I decided that a simple button should suffice. However, I can't seem to find out how to / if I can simply call the method from a button click.
The method is in home_controller.rb and the button is in index.html.erb
Any ideas? Or is this not something I can do?
The Rails controller is the logical center of your application. It coordinates the interaction between the user, the views, and the model. The controller is also a home to a number of important ancillary services.
If you look at book_controller.rb, you will find it as follows − Controller classes inherit from ApplicationController, which is the other file in the controllers folder: application.rb. The ApplicationController contains code that can be run in all your controllers and it inherits from Rails ActionController::Base class.
Ruby on Rails - Controller. The Rails controller is the logical center of your application. It coordinates the interaction between the user, the views, and the model.
The new method lets Rails know that you will create a new object. So just add the following code in this method. The above method will be called when you will display a page to the user to take user input.
<%= form_tag home_action_path, method: :post do %> <%= submit_tag 'Call Action' %> <% end %>
could also use a link
<%= link_to 'Call Action', home_action_path, method: :post %>
or you can use button_to
<%= button_to 'Call Action', home_action_path, method: :post %>
in your routes
post 'home/action'
Take a look at button_to.
Example shows you can do something like
<%= button_to "Some Button", :method=> "someButton" %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With