Reputation: 9672
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
Is the pages
part of pages#home
determined by the first part of pages_controller.rb
, ignoring the _controller.rb
end?
What happens if I change pages_controller.rb
to renamedpages_controller.rb
but leave the class name as PagesController?
thank you
Upvotes: 0
Views: 102
Reputation: 34338
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.
Upvotes: 2
Reputation: 446
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 (e.g. 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 (e.g. resources, etc) without needing to qualify each :path or :controller, and keeps URL and path helpers' usage consistent throughout your application. See Rails Guides.
Upvotes: 2
Reputation: 3561
#home
is the "action" in PagesControlleruninitialized constant PagesController
errorSo, your controllers should always be in the form NameController
defined in name_controller.rb
, and actions as public methods in NameController
.
Upvotes: 1