问题
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!
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.
I have the following in my routes.rb:
resources :sessions
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"}
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 -%>
The error I get is as follows:
ActionView::Template::Error (undefined local variable or method `sessions_path' for #<#<Class:0x109cb8900>:0x109cab840>):
It has to be something with the url_for helper as the routes do exist. What else should I look for?
UPDATE 1:
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
Isn't that is what I just did?
Update 2:
The following worked as MrYoshiji suggested:
Rails.application.routes.url_helpers.sessions_path
Update 3:
I got sessions_path working again by removing some old Rails 2 plugins in vendor directory.
回答1:
Removed some Rails 2 plugins in vendor/plugins did the trick.
来源:https://stackoverflow.com/questions/14778995/routes-stopped-working-when-upgrading-rails-3-0-to-3-1