Ruby on Rails教程,Michael Hartl第2章

时间:2015-04-20 21:17:57

标签: ruby-on-rails

在toy_app练习https://www.railstutorial.org/book/toy_app中,我在更改从通用程序运行应用程序时显示的默认消息时遇到问题:"欢迎乘坐,您在轨道上骑红宝石&# 34;世界上你好!"你好!"通过将页面路由到application_controller文件中的hello方法。 这就是我在controller / application_controller中所拥有的:

  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

和locales / 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

2 个答案:

答案 0 :(得分:2)

application_controller.rb中,将Rails.application.routes.draw do替换为class ApplicationController < ActionController::Base。看起来这条线被无意中替换为来自routes.rb的第一行的副本。

假设你有一个像app/views/layouts/application.html.erb这样的模板,你应该很高兴。

答案 1 :(得分:0)

您应该只在路线文件中放置路线。

你不想把方法放在那里。

尝试将您的方法存储在控件中