问题描述
我将Ruby 1.8.7与Rails 3.1一起使用。我的应用程序在Rails 3.0中运行良好,但是当我将其升级到Rails 3.1.4时,我所有的URL帮助程序都崩溃了!
I am using Ruby 1.8.7 with Rails 3.1. My application worked fine in Rails 3.0 but when I bumped it up to Rails 3.1.4, all my url helpers broke!
在过去2天像疯子一样谷歌搜索之后,我已经放弃了,该寻求帮助了。我不认为问题出在我的routes.rb文件上,而是更多在视图/帮助器端。
After Googling like a mad man the past 2 days, I have given up and the time has come to seek help. I don't believe the problem is with my routes.rb file but something more on the view/helper side.
我的routes.rb中包含以下内容:
I have the following in my routes.rb:
resources :sessions
在我的主页视图中,我看到以下链接_to,该错误出错了:
In my homepage view I have the following link_to, which errors out:
<%= link_to "Login", new_session_path %>
当我进行耙路时,得到以下输出,因此路径存在:
When I do rake routes, I get the following output, so the path exists:
sessions GET /sessions(.:format) {:controller=>"sessions", :action=>"index"}
POST /sessions(.:format) {:controller=>"sessions", :action=>"create"}
new_session GET /sessions/new(.:format) {:controller=>"sessions", :action=>"new"}
edit_session GET /sessions/:id/edit(.:format) {:controller=>"sessions", :action=>"edit"}
session GET /sessions/:id(.:format) {:controller=>"sessions", :action=>"show"}
PUT /sessions/:id(.:format) {:controller=>"sessions", :action=>"update"}
DELETE /sessions/:id(.:format) {:controller=>"sessions", :action=>"destroy"}
当我在浏览器中转到/ sessions / new时,页面再次加载,日一条路由存在,但在基于_path的url上出错:
When I go to /sessions/new in my browser, the page loads so again, the route exists, but it errors out on a _path based url:
<%= form_tag sessions_path, :method => :post do -%>
我得到的错误如下:
ActionView::Template::Error (undefined local variable or method `sessions_path' for #<#<Class:0x109cb8900>:0x109cab840>):
由于路由确实存在,因此必须使用url_for帮助器。我还要寻找什么?
It has to be something with the url_for helper as the routes do exist. What else should I look for?
更新1:
我在application_helper.rb中添加了以下内容:
I added the following inside application_helper.rb:
include Rails.application.routes.url_helpers
现在,当我出现以下错误时:
Now when I get the following error:
In order to use #url_for, you must include routing helpers explicitly. For instance, `include Rails.application.routes.url_helpers
这不是我刚才所做的?
更新2:
以下是Mr.Yoshiji建议的工作:
The following worked as MrYoshiji suggested:
Rails.application.routes.url_helpers.sessions_path
更新3:
我通过删除供应商目录中的一些旧的Rails 2插件再次使session_path工作。
I got sessions_path working again by removing some old Rails 2 plugins in vendor directory.
推荐答案
删除了供应商/插件中的一些Rails 2插件就可以了。
Removed some Rails 2 plugins in vendor/plugins did the trick.
这篇关于将Rails 3.0升级到3.1时,路由停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!