在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
答案 0 :(得分:2)
在application_controller.rb
中,将Rails.application.routes.draw do
替换为class ApplicationController < ActionController::Base
。看起来这条线被无意中替换为来自routes.rb
的第一行的副本。
假设你有一个像app/views/layouts/application.html.erb
这样的模板,你应该很高兴。
答案 1 :(得分:0)
您应该只在路线文件中放置路线。
你不想把方法放在那里。
尝试将您的方法存储在控件中