Question

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!

Was it helpful?

Solution

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

OTHER TIPS

DISCLAIMER: I don't write code in Rails (never did). I write Sinatra modular applications and use the MVC model.

You first need to clarify the MVC model. The MVC is an approach to programming web applications (in RoR) or user interfaces in general. So MVC stands for Model-View-Controller. I will try to explain a bit, but in order to understand this, you need to practice and play with it.

  • The Model: If you remove the layers of abstraction, it's your database scheme. The way your application interconnects in order to retrieve information.

  • The View: The way these informations are retrieved elaborated and served. Essentially is what you, or the client, see in the browser.

  • The Controller: The controller is what interacts with the program to produce a requested view or to alter a model. You request a view when you access a chart with statistical information, and you alter the model when you input DATA on it. In Rails ecosystem, ActionController is a class with a set of predefined methods to help you perform easier and quicker standard Controller actions like update a form, etc.

So the Action Controller allows you to alter data to your models (the db), or request a route to view your data, etc.

Action is not separated from controllers, it's basically what controllers do :-). Everything else is static.

If you feel that these concepts are still hard to grasp, try building a very basic modular application in Sinatra, and you will have a ground level view of how things work.

Explanation by Analogy (simple explanation without getting too technical)

I work in a busy office. I bark out orders (i.e. 'requests') to my staff to get em to do stuff.

e.g.

Sometimes I want a document so I can read it.

“Ngozi, pass me the ABC.ASX EOFY results please?”

Yes sir!

Sometimes I ask my staff to edit an existing document:

“Sunita, can you edit that report on the state of the union address?”

“Sure!” is the response.

I organise my staff based on the type of work they do

But I have a little problem.....I have 10,000s of different types of documents. Sometimes I want to get: (I) sports results and other times I want: (ii) the evening news, while still at other times I want: (iii) a collection of Donald Trump's latest 4 am Tweets.

So I created a new system. I have a staff member directly responsible for each type of thing.

  • Ngozi handles ASX (Australian Stock Exchange) Financial Results. And when I want Ngozi to do something (i.e. perform some type of action) then I tell him what to do.

  • Sunita works mainly on politics. Sometimes I”ll ask her to something (e.g. write up a report – this is one type of 'action', or I'll ask her to bring me a certain document – another type of action - and she'll do it. I like to get Sunita to work on politics and Ngozi to work on financial results. It's best to keep their responsibilities separated.).

  • And Freddie works on anything pertaining to Queen.

Etc. etc.

The meaning of the analogy?

In this case, the controller would be the person – who's responsible for handling certain types of requests. And the “action” would be the particular specific thing that I want done:

e.g.

  • getting a document or
  • edit something or even
  • creating a new document.

Hope that clears things up.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top