Why do the parameters contain information about the controller and the action?

It’s just curious that the parameters should be hashes containing information about browser requests. But debug (params) returns

controller: controllername action:actionname . . . 

Is there any specific reason these key-value pairs are contained?

+4
source share
3 answers

params comes from three sources

  • URL
  • Request String (GET)
  • Frequently Published Form Data Submit (POST)

The controller name and action name come from the URL. Say the URL is "article / 123", Rails will know:

  • ArticlesController - because the "article" is the first part of the URL
  • #show action, because the GET request is the identifier

Any controller and action can be recognized after you define them in routes.rb . Remember, you need to assign a controller name and action for a custom route?

+1
source

I'm just thinking, but it could be because Rails was built on Rack. For now, you just put these things in a Rack environment (and they are probably there right now). They probably still exist for backward compatibility.

+1
source

This way you can request those that are in the before / after / around filters, views (and not in best practice), and methods shared between multiple controllers

0
source

Source: https://habr.com/ru/post/1481303/


All Articles