【问题标题】:Problems with routing and path路由和路径问题
【发布时间】:2011-03-29 06:58:45
【问题描述】:

我是 Rails 新手,有一个奇怪的问题我不明白...

我创建了一个只有一个控制器的基本应用程序。这个控制器是命名路由(用于测试目的...),它包含索引、新建和编辑操作。

我在 routes.rb 文件中添加了一个资源:map.resources :routes

我遇到的问题是,当我尝试链接到诸如 link_to edit_route_path(some id) 之类的操作时,我收到错误 undefined local variable or method `path' for #ActionController::Routing::RouteSet:0x101f4d088>

当我直接使用 routes_path 时,它工作正常。

感谢您的帮助!

rake 路由的输出:

路由 GET /routes(.:format) {:controller=>"routes", :action=>"index"}
POST /routes(.:format) {:controller=>"routes", :action=>"create"}
new_route GET /routes/new(.:format) {:controller=>"routes", :action=>"new"}
edit_route GET /routes/:id/edit(.:format) {:controller=>"routes", :action=>"edit"}
route GET /routes/:id(.:format) {:controller=>"routes", :action=>"show"}
PUT /routes/:id(.:format) {:controller=>"routes", :action=>"update"}
删除 /routes/:id(.:format) {:controller=>"routes", :action=>"destroy"}
/:controller/:action/:id
/:controller/:action/:id(.:format)

这是我遇到的错误:

#ActionController::Routing::RouteSet:0x101f4d128 的未定义局部变量或方法“路径”>

和堆栈跟踪:

/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/routing/route_set.rb:386:in generate'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/url_rewriter.rb:208:in
rewrite_path'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/url_rewriter.rb:187:in rewrite_url'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/url_rewriter.rb:165:in
rewrite'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:625:in url_for'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/url_helper.rb:85:in
send'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/url_helper.rb:85:in url_for'
(eval):17:in
edit_blog_path'
/Users/guillaume/Projets/rails/testroutes/app/views/blogs/edit.html.erb:4:in `_run_erb_app47views47blogs47edit46html46erb'

ruby 版本是 1.8.7 宝石版本是 1.3.7 rails 版本是 2.3.8

我尝试了 rails 入门用户指南中的基本帖子脚手架,当我在新页面或编辑页面时遇到同样的错误...

ActionController::Routing::Routes.draw 做 |map|
地图资源:路线
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
结束

奇怪的是上周一切正常,我不知道我有什么改变......

非常感谢!!!

【问题讨论】:

  • 我的结果与 sachinrathore 相同,您能否在帖子中添加 routes.rb 文件(请减去 cmets)和生成错误的代码,以及您将这段代码放在哪里.我对返回路由集然后在其上抛出path 感到有点困惑。另外您使用的是哪个版本的导轨?
  • 我目前无法访问我的项目,但我的 routes.rb 非常基础。它包含文件末尾的默认路由和一个 map.resources :routes,仅此而已。对于版本,我有最新的(2.3.8)
  • 好吧,我也是,无法复制。如果没有看到代码,我不确定,但要检查的一件事是您没有意外遇到任何保留名称 - 也许是使用 gem 或其他东西。尝试将其重命名为有点离谱的东西,以确保确定。所以映射一个资源map.resources :angus 看看你是否能够以这种方式复制。
  • 更正,map.resources :anguses 除非 angus 的复数形式是 angi?任何人? ;)

标签: ruby-on-rails routing path


【解决方案1】:

转到终端并输入

rake routes

它将显示您在 routes.rb 文件中定义的所有可能的路由。 然后检查edit_route_path是否存在

【讨论】:

  • 我在问题中添加了 rake 路由的输出
  • 我已经检查了我当地的作品。我 edit_route_path(1) 它给出路径 /routes/1/edit
  • 这很奇怪,我真的不明白这个,它应该可以找到,我想知道它是否与我的 rails 和 ruby​​ 版本无关......你有什么版本轨道?
猜你喜欢
  • 2021-10-23
  • 1970-01-01
  • 1970-01-01
  • 2019-09-13
  • 2015-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多