Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 - index action not loading by default on controller

Now i have a fresh rails 3 install running over rvm 1.9.2 I generated a controller using the follow instruction:

rails generate controller blog index

The output is

      create  app/controllers/blog_controller.rb
      route  get "blog/index"
      invoke  erb
      create    app/views/blog
      create    app/views/blog/index.html.erb
      invoke  test_unit
      create    test/functional/blog_controller_test.rb
      invoke  helper
      create    app/helpers/blog_helper.rb
      invoke    test_unit
      create      test/unit/helpers/blog_helper_test.rb

but in browser when i try to get to http://localhost:3000/blog i get:

No route matches "/blog"

but if i type http://localhost:3000/blog/index

it renders the index view.

doesn't it works like Rails 2? where i get to the index view by default with just putting the controller name on the url ?

thanks.

like image 984
Mr_Nizzle Avatar asked Apr 26 '11 17:04

Mr_Nizzle


1 Answers

If you look into routes.rb you'll see

get "/blog/index" => "blog#index"

So just remove it with

get "/blog" => "blog#index"

or you can use resources here.

But only question: why do you use singular form? It is nonsensical to call index to singular noun. You should use or "blog#show" as a resource or "blogs#index" as a resources.

Conventions in Rails is a kind of basement. Don't break them if you can follow them

like image 97
fl00r Avatar answered Nov 06 '22 07:11

fl00r