[英]Ruby on Rails Tutorial, Michael Hartl Chapter 2
在toy_app练习https://www.railstutorial.org/book/toy_app中,我在将运行应用程序时显示的默认消息从通用名称更改为“出现问题:“欢迎乘坐,您正在将红宝石骑在铁轨上”改为“你好,世界!” 通过将页面路由到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
在application_controller.rb
,将Rails.application.routes.draw do
替换为class ApplicationController < ActionController::Base
。 看起来这行被无意中替换为routes.rb
的第一行的routes.rb
。
假设您已定义了一个类似app/views/layouts/application.html.erb
的模板,那么您应该会做得很好。
您只应将路由放入路由文件中。
您不想在其中放置方法。
尝试将方法存储在控件中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.