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

前端 未结 3 1592
误落风尘
误落风尘 2021-02-03 09:57

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

I fetched this definition from the official rails guide:

A co

3条回答
  •  佛祖请我去吃肉
    2021-02-03 10:54

    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.

提交回复
热议问题