I noticed that an index view is routed correctly even if there isn't a controller method index.
As an example, the routes.rb
has this route
AppName::Application.routes.draw do
get 'about' => "about#index"
end
My controller looks like this with no index method (def index end
)
class AboutController < ApplicationController
end
and I have a view called index.html.erb
in the views/about folder
What's happening here? Is this a case of rails magic where they automatically show the view even if there is no controller method? I couldn't find any documentation on this...
The index method in a controller is used to return a collection of models. For example, an index method inside PostsController should return a collection of Post models.
The Rails controller is the logical center of your application. It coordinates the interaction between the user, the views, and the model. The controller is also a home to a number of important ancillary services. It is responsible for routing external requests to internal actions.
Difference between singular resource and resources in Rails routes. So far, we have been using resources to declare a resource. Rails also lets us declare a singular version of it using resource. Rails recommends us to use singular resource when we do not have an identifier.
Rails filters are methods that run before or after a controller's action method is executed. They are helpful when you want to ensure that a given block of code runs with whatever action method is called.
If you have the view file, it'll go ahead and render that implicitly, as documented here
See also, this SO thread on how Rails renders your view files and controller actions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With