In
http://guides.rubyonrails.org/action_controller_overview.html
I read that
Rails creates an instance of that controller and runs the method with the same name as the action.
so i don't understand, what is difference between actions and public methods in controller. Actions and public methods in controller are not the same?
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. It is responsible for routing external requests to internal actions.
1 What Does a Controller Do? Action Controller is the C in MVC. After the router has determined which controller to use for a request, the controller is responsible for making sense of the request and producing the appropriate output.
Ruby on Rails defines seven standard controller actions can be used to do common things such as display and modify data. If you only want to create routes for specific actions, you can use :only to fine tune the resource route. This line maps URLs only to the Message controller's index and show actions.
As the two answers said, you should not be calling controller methods from your models. It is not recommended.
A controller is a Ruby class which inherits from ApplicationController and has methods just like any other class. When your application receives a request, the routing will determine which controller and action to run, then Rails creates an instance of that controller and runs the method with the same name as the action.
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.
When your application receives a request, the routing will determine which controller and action to run, then Rails creates an instance of that controller and runs the method with the same name as the action. class ClientsController < ApplicationController def new end end
After routing has determined which controller to use for a request, your controller is responsible for making sense of the request and producing the appropriate output. Luckily, Action Controller does most of the groundwork for you and uses smart conventions to make this as straightforward as possible.
Action is also a method but it has a corresponding route, you can hit an action by using it's route but you can't call method an action if it doesn't have any route associated with it.
e.g. In rails new, index, create, show, update, delete and edit are default actions, because all these methods have routes associated with them. But if you define a method in the controller which is called by an action but it doesn't have any route associated with it then its a method but not an action.
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