16

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!

mobesa
  • 271
  • 3
  • 9
  • 1
    Here is a link to the guide: http://guides.rubyonrails.org/getting_started.html – mobesa Jan 04 '14 at 11:03
  • take the help from this link http://stackoverflow.com/questions/6216722/difference-between-resource-and-controller-generators – HarsHarI Jan 04 '14 at 11:06
  • 1
    This is very confusing HarsHarl. Too much code! I am looking for a bit of an 'abstract' explanation. Thanks by the way! – mobesa Jan 04 '14 at 11:08
  • 1
    Yeah, I ran into those two paragraph under 4.2 "Hello Rails" and read them over and over. They are extremely confusing and unclear, but I knew they hold the most crucial big-picture information. This thread is invaluable. – LazerSharks Dec 19 '14 at 04:49

3 Answers3

20

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

Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • 1
    No problem - it's an incredible system to learn, but the more you find out, the more growth you can see! How long have you been developing RoR for? – Richard Peck Jan 04 '14 at 11:27
  • 2
    Because your question is generic - SO is really for specific programming questions (the more code, the better). I'll upvote you again – Richard Peck Jan 04 '14 at 11:27
  • 2
    *it tells Rails to load the `new` function in the `users` controller*-- not a function..It is a method. :) :) Be Rubyish.. – Arup Rakshit Jan 04 '14 at 11:29
  • Thanks a heap! I am very new to RoR! And to programming in general. I am very grateful Rich! – mobesa Jan 04 '14 at 11:29
  • @ArupRakshit Sorry buddy - I've used several languages, so `function` is just my way of explaining :) – Richard Peck Jan 04 '14 at 11:31
  • 3
    A great big picture explanation for noobs like me that need some idea of how it all works. A+ thank you – Steve Apr 15 '14 at 01:23
5

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.

patm
  • 1,420
  • 14
  • 19
1

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.

BenKoshy
  • 33,477
  • 14
  • 111
  • 80