Rails 3 - 在控制器上默认不加载索引操作

Mr_*_*zle 6 routes ruby-on-rails-3

现在我有一个新的rails 3安装运行rvm 1.9.2 我使用以下指令生成一个控制器:

rails generate controller blog index
Run Code Online (Sandbox Code Playgroud)

输出是

      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
Run Code Online (Sandbox Code Playgroud)

但在浏览器中,当我试图得到http://localhost:3000/blog我得到:

No route matches "/blog"
Run Code Online (Sandbox Code Playgroud)

但如果我打字 http://localhost:3000/blog/index

它呈现索引视图.

它不像Rails 2那样有效吗?我在默认情况下进入索引视图,只需将控制器名称放在URL上?

谢谢.

fl0*_*00r 10

如果你看着routes.rb你会看到

get "/blog/index" => "blog#index"
Run Code Online (Sandbox Code Playgroud)

所以只需将其删除即可

get "/blog" => "blog#index"
Run Code Online (Sandbox Code Playgroud)

或者你可以resources在这里使用

但唯一的问题是:你为什么使用单数形式?打电话indexsingular名词是没有意义的.您应该使用或"blog#show"作为资源或"blogs #index"作为资源.

Rails中的约定是一种地下室.如果你能遵循它们,不要打破它们


小智 4

对于轨道 3:

match '/blog', :controller => 'blog', :action => 'index'
Run Code Online (Sandbox Code Playgroud)