Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails doesn't need index method in controller defined?

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...

like image 549
hajpoj Avatar asked Dec 22 '11 04:12

hajpoj


People also ask

What is a index method in Rails?

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.

What does controller do in Rails?

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.

What is the difference between resources and resource in Rails?

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.

What is filter in Rails?

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.


1 Answers

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.

like image 75
sczizzo Avatar answered Oct 18 '22 20:10

sczizzo