【问题标题】:Ruby on Rails say/hello throws routing errorRuby on Rails say/hello 抛出路由错误
【发布时间】:2015-01-17 23:57:32
【问题描述】:

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

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

当我转到演示目录并手动转到 routes.rb 文件时,我注意到一件事,除了我输入的 match 命令的注释内容之外什么都没有。这会是它不起作用的原因吗?我保存在 aptana 中不应该反映这一点吗?

【问题讨论】:

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


    【解决方案1】:

    你需要在你的路由文件中:

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

    而且你可以取出match方法。注释掉的代码仅供参考,Ruby 解释器会忽略它。您发布的示例代码中失败的主要原因是前两个 get 方法没有指向控制器和操作。

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

    【讨论】:

    • 从我遵循“使用 rails 4 进行敏捷 Web 开发”的教程中,他们创建了一个演示应用程序,然后使用命令“rails generate controller Say hello goodbye”创建了一个控制器,然后所有示例都显示在之后那是 localhost:3000/say/hello 并且网页直接打开了,没有提到对 routes.rb 的修改......为什么我的很时髦?
    • 你需要下划线。例如rails g 控制器 say_hello_goodbye
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多