Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between controllers and actions in ruby on rails?

Can anybody tell me the difference between controllers and actions in ruby on rails?

I fetched this definition from the official rails guide:

A controller's purpose is to receive specific requests for the application. Routing decides which controller receives which requests. Often, there is more than one route to each controller, and different routes can be served by different actions. Each action's purpose is to collect information to provide it to a view.

I am confused. Please, make it as simple as possible since I am newbie!

Thanks!

like image 225
mobesa Avatar asked Jan 04 '14 11:01

mobesa


People also ask

What are controllers in Ruby on 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 a controller action Rails?

In the Rails architecture, Action Controller receives incoming requests and hands off each request to a particular action. Action Controller is tightly integrated with Action View; together they form Action Pack. Action Controllers, or just “controllers,” are classes that inherit from ActionController::Base .

What is before action in Ruby on Rails?

When writing controllers in Ruby on rails, using before_action (used to be called before_filter in earlier versions) is your bread-and-butter for structuring your business logic in a useful way. It's what you want to use to "prepare" the data necessary before the action executes.

How do you call a controller method in Rails?

You can use url_for to get the URL for a controller and action and then use redirect_to to go to that URL.


1 Answers

Controllers are just Ruby Class files which have a series of instance methods inside


Basic Explanation

Rails controllers are basically files where actions (methods) are kept

Each time you access a Rails app, you're sending a request to the system. The various technologies inside Rails route that request to a certain action, where your code can use the passed data to perform some sort of action (hence the name). The actions are kept inside controllers to give the application structure

So if you access http://yourapp.com/users/new, it tells Rails to load the new method in the users controller. You can have as many actions in the controllers as you want, but you have to tell the Rails routes system they are there, otherwise they won't be accessible


Proper Explanation

Rails Controllers are just Ruby Classes, storing a series of actions

The "actions" (instance methods) work on passed data (params) to create objects that can either be passed to the model, or used inside other methods

Whenever you send a request to Rails (access a URL), it first uses the ActionDispatch middleware to send your request to the correct Class (controller) instance method (action), and then your code does something with that data

Your job as a dev is to connect the right controllers with the right models, presenting the right data the user at the right time

like image 184
Richard Peck Avatar answered Sep 23 '22 22:09

Richard Peck