2015-04-20 141 views
0

在toy_app練習https://www.railstutorial.org/book/toy_app中,我遇到了一個問題:當從泛型運行應用程序時,顯示的默認消息顯示爲「歡迎登上,您正在軌道上使用紅寶石」你好,世界!」通過將頁面路由到application_controller文件中的hello方法。 這是我在控制器/ application_controller:Ruby on Rails教程,Michael Hartl第2章

Rails.application.routes.draw do 
    # Prevent CSFR attacks by raising an exception. 
    # For API's, you may want to use :null_session instead 
    protect_from_forgery with: :exception 

    def hello 
    render text: "hello, world!" 
    end 

end 

和語言環境/ routes.rb中

Rails.application.routes.draw do 

    # 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 'application#hello' 

    # 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 
+0

在'application_controller.rb'中刪除此行:'Rails.application.routes.draw do'(它不屬於那裏) – Matt

+0

請不要downvote初學者問題。如果它是重複的,則將其標記爲這樣。否則回答或移動。 –

回答

0

您應該只把航線路線文件。

你不想把方法放在那裏。

嘗試在controlle

2

存儲你的方法在application_controller.rb,與class ApplicationController < ActionController::Base更換Rails.application.routes.draw do。看起來這條線被無意中替換爲routes.rb的第一行的副本。

假設你有一個如app/views/layouts/application.html.erb定義的模板,你應該很好去。

相關問題