You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails中使用resources定义articles路由后无法访问/new路径的问题求助

Rails中使用resources定义articles路由后无法访问/new路径的问题求助

你好呀!先帮你梳理下问题所在,你遇到的No route matches [GET] "/new"错误,大概率是两个常见的小问题导致的,咱们一步步来排查:

1. 访问路径搞错了——RESTful路由的路径规则

你在routes.rb里用了resources :articles,这会生成标准的RESTful路由,其中new动作对应的路径是**/articles/new**,而不是你现在访问的根路径/new。之前你注释掉的get 'new', to:'articles#new'才是对应/new路径的,现在换成resources后,路径规则已经变啦。

2. 修改路由后没重启服务器

如果执行rails routes --expanded都看不到articles相关的路由,那大概率是你修改routes.rb后,没有重启Rails开发服务器。Rails在运行时会缓存路由配置,修改后必须重启服务器,新的路由规则才会生效。

接下来的解决步骤:

  • 先关闭当前运行的Rails服务器(终端按Ctrl+C),然后重新启动rails s
  • 打开终端执行rails routes,你应该能看到类似下面的articles相关路由:
    articles GET    /articles(.:format)          articles#index
             POST   /articles(.:format)          articles#create
    new_article GET    /articles/new(.:format)      articles#new
    edit_article GET    /articles/:id/edit(.:format) articles#edit
    article GET    /articles/:id(.:format)      articles#show
             PATCH  /articles/:id(.:format)      articles#update
             PUT    /articles/:id(.:format)      articles#update
             DELETE /articles/:id(.:format)      articles#destroy
    
  • 然后访问正确的路径http://localhost:3000/articles/new,应该就能正常加载你的表单页面了

另外,你的表单代码里url: articles_path是对的,因为articles_path对应POST请求的/articles,正好匹配create动作,这部分没问题。

如果还是有问题,可以再检查下routes.rb有没有语法错误,比如有没有漏掉结尾的end之类的,不过从你贴的代码看是正常的。

备注:内容来源于stack exchange,提问作者Mr.DevEng

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 09:53:05