Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Action and method in Rails controller

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?

like image 564
igor_rb Avatar asked Feb 21 '14 06:02

igor_rb


People also ask

What are the controller actions in Rails?

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.

What is action in controller Ruby?

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.

What are actions in Ruby on Rails?

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.

Can we call controller method in model Rails?

As the two answers said, you should not be calling controller methods from your models. It is not recommended.

What is the use of a controller in rails?

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.

What is the difference between actioncontroller and ApplicationController in rails?

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.

How does routing work in rails?

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

How does action controller work with routing?

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.


1 Answers

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.

like image 174
Rajdeep Singh Avatar answered Oct 12 '22 00:10

Rajdeep Singh