【问题标题】:Routing Error after scaffolding and migration脚手架和迁移后的路由错误
【发布时间】:2012-05-07 07:49:07
【问题描述】:

我创建了一个没有问题的脚手架

$ rails 生成脚手架新名称:字符串标题:字符串内容:文本

运行迁移的 Rake 命令(没有问题,表正确创建)

$ rake db:迁移

编辑 app/views/home/index.html.erb

'新闻' %>

我在“http://localhost:3000”中正确看到了主页和链接;点击链接“我的新闻”页面“http://localhost:3000/news”加载没有错误。

现在,点击 Rails 生成的链接“New New”,链接的目标 localhost:3000/news/new (source "New New"),我阅读此错误:

路由错误

没有路由匹配 {:action=>"show", :controller=>"news", :format=>nil}

尝试运行 rake 路线以获取有关可用路线的更多信息。

在“app/views/news/index.html.erb”中的链接源是

在 routes.rb 我读过

MyApp::Application.routes.draw 做

资源:新闻

获取“主页/索引”

  • Rails 3.2.3
  • Ruby 1.9.3p125
  • MySQL 5.5
  • Windows 7 64 位

耙路线:

news_index GET /news(.:format) news#index

POST /news(.:format) news#create

new_news GET /news/new(.:format) news#new

edit_news GET /news/:id/edit(.:format) news#edit

新闻 GET /news/:id(.:format) news#show

PUT /news/:id(.:format) news#update

删除 /news/:id(.:format) news#destroy

home_index GET /home/index(.:format) home#index

根目录/home#index

提前谢谢我的英语对不起

【问题讨论】:

  • 生成的链接的目标是什么?你能从生成的源中复制那行吗?
  • 是的,链接的目标是localhost:3000/news/new

标签: ruby-on-rails-3 routing scaffolding


【解决方案1】:

您必须使用news_index_path,因为如果rails 不能生成单数,则新闻不是单数- prular 区分它们将在末尾添加_index :)

您有一个news 和多个news,这令人困惑。

总是尝试使用<name_of_resource>_path 来生成url :)

news_index GET /news(.:format) news#index

这表示它是隐含的,你使用 1 部分 news_index 并添加 _path 来获取它的路径。

你应该有

<%= link_to 'My News', news_index_path %>

希望对您有所帮助,干杯!

【讨论】:

  • 谢谢,但我在“app/views/home/index.html.erb”中用“news_index_path”更改了“:controller => 'news'”,点击“New”时出现同样的错误新的”。在“app/views/news/index.html.erb”中,链接是“”
猜你喜欢
  • 2014-11-28
  • 2021-12-09
  • 2017-03-21
  • 1970-01-01
  • 2023-01-13
  • 2018-02-12
  • 2011-11-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多