【发布时间】: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'rewrite_path'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/url_rewriter.rb:208:in
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/url_rewriter.rb:187:inrewrite_url'rewrite'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/url_rewriter.rb:165:in
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_controller/base.rb:625:inurl_for'send'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/url_helper.rb:85:in
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/url_helper.rb:85:inurl_for'edit_blog_path'
(eval):17:in
/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