Ruby on Rails说/ hello抛出路由错误

时间:2015-01-17 23:57:33

标签: ruby-on-rails ruby ruby-on-rails-3

大家好我试图熟悉rails,我在Windows 7上使用aptana studio 3创建了一个说控制器。当我去'localhost:3000 / say / hello'时,它给我一个路由错误,没有指定路由,等等.. 所以我在网上找了几个解决方案,我试图逐个应用它们,现在我的路线似乎没有任何效果

Rails.application.routes.draw do
  get 'say/hello'

  get 'say/goodbye'

  #map.connect 'say/hello', :controller => 'say', :action => 'hello'
  #map.connect ':controller/:action/:id'
  #map.connect ':controller/:action/:id.:format'
  #match ':controller(/:action(/:id))(.:format)'
  #link_to "hello", :controller => "mycontroller", :action => "myaction"
  match '/say/hello' => 'Say#hello', :as => 'say_hello'
  #root :to => 'Say#hello'

  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
  # root 'welcome#index'

  # Example of regular route:
  #   get 'products/:id' => 'catalog#view'

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

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

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

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

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

  # Example resource route with concerns:
  #   concern :toggleable do
  #     post 'toggle'
  #   end
  #   resources :posts, concerns: :toggleable
  #   resources :photos, concerns: :toggleable

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

有一件事我注意到当我去demo目录并手动转到routes.rb文件时,除了注释的东西之外什么都没有,我放的匹配命令不存在。这会是它不工作的原因吗?我在aptana中保存了它不应该反映出来吗?

1 个答案:

答案 0 :(得分:0)

您需要在路线文件中使用此功能:

get 'say/hello' => "say#hello", as: :say_hello
get 'say/goodbye' => "say#goodbye", as: :say_goodbye

你可以拿出匹配方法。注释掉的代码仅供参考,Ruby解释器将忽略它们。它们发布的示例代码失败的主要原因是前两个方法没有指向控制器和操作。

这是指向Rails路由文档的链接: http://guides.rubyonrails.org/routing.html