更新Rails项目后网站输出无变化的技术求助
Rails访问/articles页面无变化的排查方案
以下是针对问题的排查步骤,按顺序检查:
1. 确认服务器已完全重启
修改路由、控制器或视图后,必须重启开发服务器才能生效。按下Ctrl+C终止当前服务器进程,重新运行rails s启动服务器,再访问目标地址。
2. 验证路由是否正确加载
在终端运行rails routes命令,查看输出中是否存在如下条目:
articles GET /articles(.:format) articles#index
如果没有这条记录,说明config/routes.rb可能存在语法错误,仔细检查文件内容是否符合Rails路由规范。
3. 检查视图文件路径与名称
确保视图文件路径为app/views/articles/index.html.erb,注意:
- 文件夹名称必须是
articles(小写复数,与控制器名对应) - 文件名必须是
index.html.erb(与控制器中的index方法对应,后缀.html.erb不能省略) - 系统大小写敏感(Linux/macOS环境),避免出现
Articles、Index.html.erb这类错误命名
4. 排除浏览器缓存干扰
按下Ctrl+F5强制刷新页面,或使用浏览器隐私模式访问,避免加载缓存的旧页面。
你当前的代码核对
config/routes.rb
Rails.application.routes.draw do # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500. # Can be used by load balancers and uptime monitors to verify that the app is live. get "up" => "rails/health#show", as: :rails_health_check # Defines the root path route ("/") # root "posts#index" get "/articles", to: "articles#index" end
这段路由配置正确,已将/articles映射到ArticlesController的index方法。
app/controllers/articles_controller.rb
class ArticlesController < ApplicationController def index end end
控制器代码无问题,index方法会自动渲染对应的视图文件。
app/views/articles/index.html.erb
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <style> /* Email styles need to be inline */ </style> </head> <body> <h1>Hello, Rails!</h1> </body> </html>
视图文件内容正确,只要路径匹配,应该能正常渲染出Hello, Rails!。
内容的提问来源于stack exchange,提问作者K Man
相关产品推荐
相关产品推荐

