Rails 3 - index action does not load by default on the controller

Now I have a new installation of rails 3 working on rvm 1.9.2 I generated the controller using the following command:

 rails generate controller blog index 

Output signal

  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 the 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 displays an index.

Does this work like Rails 2? where do i get the default index by simply putting the controller name on the url?

thanks.

+6
source share
6 answers

For rails 3:

 match '/blog', :controller => 'blog', :action => 'index' 
+4
source

If you look at routes.rb , you will see

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

So just delete it with

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

or you can use resources here.

But just the question: why are you using a single form? It makes no sense to call a noun index to singular . You should use either "blog # show" as a resource or "blog # index" as resources.

Rails conventions are a kind of basement. Do not break them if you can follow them.

+10
source

Rails generate does not generate resources for your controller by default. You specified one action for your controller, "index", so in your case you will get this in config / routes.rb:

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

The simplest task would be to change this to:

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

Ian.

+4
source

This is an assumption based on my experience with Rails 2, but here's what I think happens:

If you created your controller with the scaffold parameter (which is still in Rails 3, right?), It would create a model in addition to your controller and add the appropriate routes by calling map.resources (or the equivalent of Rails 3) - this last bit is what gives you the routes /models you expect.

But since you just generated the controller, the model was not created, and thus Rails doesn’t put in the map.resources operator in routes.rb - map.resources really only make sense when there is a model underlying your controller . In fact, I do not think that it adds any special routes when creating the controller; you get to your index along one of the default routes: /:controller/:action .

So, if you want to go to your index with /blog , you will have to add the route yourself. Fortunately, it has to be single-line.

Hope this helps!

PS: If you are paranoid, you need to disable these routes by default before you start production - they allow GET requests to trigger actions that modify your database (for example, GET:/blog/destroy ), opening you before the attack fake requests .

+2
source

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

+1
source

Instead of manually routing, you can go to /app/controllers/application_controller.rb and add an empty index method

 def index end 

make sure that your generated controller extends the application controller and forces all the controllers you create to do what you want. Tested on Rails 3.2. *

0
source

Source: https://habr.com/ru/post/886725/


All Articles