2

我有

controller : Home_controller
action : index

在我的 routes.rb 文件中,如何配置我home/index的被调用文件而不是默认index.html文件作为应用程序的第一页

我的家庭控制器文件是

class HomeController < ApplicationController
  def index
  end
end

我的 routes.rb 文件是

RailsBegin::Application.routes.draw do
  root :to => "home#index"
  #get "home/index"

  # The priority is based upon order of creation:
  # first created -> highest priority.

  # Sample of regular route:
  #   match 'products/:id' => 'catalog#view'
  # Keep in mind you can assign values other than :controller and :action

  # Sample of named route:
  #   match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
  # This route can be invoked with purchase_url(:id => product.id)

  # Sample resource route (maps HTTP verbs to controller actions automatically):
  #   resources :products

  # Sample resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end

  # Sample resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end

  # Sample resource route with more complex sub-resources
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get 'recent', :on => :collection
  #     end
  #   end

  # Sample resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end

  # You can have the root of your site routed with "root"
  # just remember to delete public/index.html.
  # root :to => 'welcome#index'

  # See how all your routes lay out with "rake routes"

  # This is a legacy wild controller route that's not recommended for RESTful applications.
  # Note: This route will make all actions in every controller accessible via GET requests.
  # match ':controller(/:action(/:id))(.:format)'
end

堆栈跟踪 :

d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/routing/mapper.rb:179:in `default_controller_and_action': missing :action (ArgumentError)
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/routing/mapper.rb:72:in `normalize_options!'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/routing/mapper.rb:60:in `initialize'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/routing/mapper.rb:1308:in `new'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/routing/mapper.rb:1308:in `add_route'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/routing/mapper.rb:1287:in `decomposed_match'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/routing/mapper.rb:1269:in `block in match'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/routing/mapper.rb:1269:in `each'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/routing/mapper.rb:1269:in `match'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/routing/mapper.rb:254:in `root'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/routing/mapper.rb:1321:in `root'
    from D:/WorkSpace/Eclipse_WorkSpace/Rails_begin/config/routes.rb:2:in `block in <top (required)>'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:289:in `instance_exec'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:289:in `eval_block'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:267:in `draw'
    from D:/WorkSpace/Eclipse_WorkSpace/Rails_begin/config/routes.rb:1:in `<top (required)>'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `load'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `block in load'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:236:in `load_dependency'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `load'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/application/routes_reloader.rb:40:in `each'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/application/routes_reloader.rb:40:in `load_paths'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/application/routes_reloader.rb:16:in `reload!'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/application/routes_reloader.rb:26:in `block in updater'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/file_update_checker.rb:78:in `call'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/file_update_checker.rb:78:in `execute'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/application/routes_reloader.rb:27:in `updater'
    from d:in `execute_if_updated'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/application/finisher.rb:66:in `block in <module:Finisher>'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/initializable.rb:30:in `instance_exec'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/initializable.rb:30:in `run'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/initializable.rb:55:in `block in run_initializers'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/initializable.rb:54:in `each'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/initializable.rb:54:in `run_initializers'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/application.rb:136:in `initialize!'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from D:/WorkSpace/Eclipse_WorkSpace/Rails_begin/config/environment.rb:5:in `<top (required)>'
    from D:/WorkSpace/Eclipse_WorkSpace/Rails_begin/config.ru:3:in `require'
    from D:/WorkSpace/Eclipse_WorkSpace/Rails_begin/config.ru:3:in `block in <main>'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/rack-1.4.5/lib/rack/builder.rb:51:in `instance_eval'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/rack-1.4.5/lib/rack/builder.rb:51:in `initialize'
    from D:/WorkSpace/Eclipse_WorkSpace/Rails_begin/config.ru:in `new'
    from D:/WorkSpace/Eclipse_WorkSpace/Rails_begin/config.ru:in `<main>'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/rack-1.4.5/lib/rack/builder.rb:40:in `eval'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/rack-1.4.5/lib/rack/builder.rb:40:in `parse_file'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/rack-1.4.5/lib/rack/server.rb:200:in `app'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/server.rb:46:in `app'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/rack-1.4.5/lib/rack/server.rb:304:in `wrapped_app'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/rack-1.4.5/lib/rack/server.rb:254:in `start'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/server.rb:70:in `start'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:55:in `block in <top (required)>'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:50:in `tap'
    from d:/BitNami/rubystack-1.9.3-10/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:50:in `<top (required)>'
    from D:/WorkSpace/Eclipse_WorkSpace/Rails_begin/script/rails:6:in `require'
    from D:/WorkSpace/Eclipse_WorkSpace/Rails_begin/script/rails:6:in `<main>'
4

1 回答 1

2

首先,删除该默认index.html文件。然后添加您的路线:

root :to => "home#index"

我假设home_controller.rb您的目录中有文件app/controllers,看起来或多或少像这样:

class HomeController < ApplicationController

  def index
  #stuff
  end

end
于 2013-06-05T12:01:54.037 回答