Mr_Nizzle
Mr_Nizzle

Reputation: 6714

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.

Upvotes: 6

Views: 6509

Answers (6)

axeltaglia
axeltaglia

Reputation: 2593

For rails 3:

match '/blog', :controller => 'blog', :action => 'index'

Upvotes: 4

Theofilos Mouratidis
Theofilos Mouratidis

Reputation: 1156

Instead of manual routing you can go to /app/controllers/application_controller.rb and add a blank index method

def index
end

make sure your generated controller extends the application controller, and boom all your generated controllers do what you want
Tested on Rails 3.2.*

Upvotes: 0

MT.
MT.

Reputation: 1925

Add this to your routes.rb file match ':controller(/:action(/:id))(.:format)'
It's better if you add it at the bottom of the routes.rb file.
The problem with this approach is that it will make all your actions available through get request. So be careful with that.

Upvotes: 1

ipd
ipd

Reputation: 5714

Rails generate does not generate resources for your controller by default. You specified one action for your controller, 'index', so in your case you end up with this in config/routes.rb:

Blog::Application.routes.draw do
    get "blog/index"

The simplest thing to do would be to change this to:

  get "blog", :to => 'blog#index'

ian.

Upvotes: 4

fl00r
fl00r

Reputation: 83680

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

Upvotes: 10

Xavier Holt
Xavier Holt

Reputation: 14619

This is a guess, based on my experience with Rails 2, but here's what I think is happening:

If you'd generated your controller with the scaffold option (that's still in Rails 3, right?), it would have created a model in addition to your controller, and added the corresponding routes via a call to map.resources (or Rails 3 equivalent) - this last bit is what gives you the /models routes you're expecting.

But since you just generated the controller, no model was created, and thus Rails doesn't put in a map.resources statement in routes.rb - map.resources really only makes sense when there's a model underlying your controller. In fact, I don't think it adds any special routes when you generate a controller; you're getting to your index by one of the default routes: /:controller/:action.

So if you want to get to your index from /blog, you'll have to add the route yourself. Luckily, it should be a one-liner.

Hope this helps!

PS: And if you're paranoid, you'll want to disable those default routes before you go to production - they allow GET requests to trigger actions that change your database (e.g. GET:/blog/destroy), opening you up to Cross-Site Request Forgery attacks.

Upvotes: 2

Related Questions