简体   繁体   中英

what determines controller action ruby rails

at https://www.codecademy.com/en/courses/learn-rails/lessons/start/exercises/start-views , the controll action is described as 'pages#home':

Well done! Now when a user visits http://localhost:8000/welcome, the route

get 'welcome' => 'pages#home'

will tell Rails to send this request to the Pages controller's home action.

but when I made the controller I did rails generate controller Pages which is uppercase.

pages_controller.rb:

class PagesController < ApplicationController

  def home
  end

end
  1. Is the pages part of pages#home determined by the first part of pages_controller.rb , ignoring the _controller.rb end?

  2. What happens if I change pages_controller.rb to renamedpages_controller.rb but leave the class name as PagesController?

thank you

When a request is made like: http://localhost:8000/welcome , it matches a route in the routes.rb file where the route is mapped to a controller and an action .

In your routes file, you have:

get 'welcome' => 'pages#home'

get 'welcome' part matches with the url .../welcome and map this request to pages#home where pages is the controller name and home is an action (a method defined in pages_controller ). This is Rails convention to name the controllers like this: ControllerName_controller.rb . So, basically pages is your controller's name and the last _controllers is common to each and every controller in your Rails application.

What happens next is, this action of the controller do some work and talk to the model / database and build the required variables/data that will be passed to a view . According to Rails convention, there has to be a matching view file like: home.html.erb which will render the response to the browser.

There are other ways to render different partials and views in different ways, but if you follow the Rails convention, then it becomes very easy to understand and work with different Models, Views and Controllers in Rails. This is called: Convention Over Configuration . When you follow the Rails convention for naming things, Rails does a lot of work and configuration for you for free to make your life easier.

When you have get 'welcome' => 'pages#home' in your routes file, then when a request: /welcome comes, it maps to pages_controller and it will look for the pages_controller.rb file under the app/controller/ . If you rename this to something else, then the program will not find it as it expected and will throw you an error and your request will not be completed.

Your controller's name has to match with the class name of that controller, This is also a Rails convention. If you change any one of them, then you will get an error and your request will fail.

Action Controller is the C in MVC. After routing has determined which controller to use for a request, your controller is responsible for making sense of the request and producing the appropriate output. Luckily, Action Controller does most of the groundwork for you and uses smart conventions to make this as straightforward as possible.

Controller Naming Convention

The naming convention of controllers in Rails favors pluralization of the last word in the controller's name, although it is not strictly required (eg ApplicationController). For example, ClientsController is preferable to ClientController, SiteAdminsController is preferable to SiteAdminController or SitesAdminsController, and so on.

Following this convention will allow you to use the default route generators (eg resources, etc) without needing to qualify each :path or :controller, and keeps URL and path helpers' usage consistent throughout your application. See Rails Guides .

  1. Yes. and #home is the "action" in PagesController
  2. You get a uninitialized constant PagesController error

So, your controllers should always be in the form NameController defined in name_controller.rb , and actions as public methods in NameController .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM