Rails 升级:服务器说路由不存在,而实际上它存在!
给出:
以下部分:
<%= form_for @user_session, :url => user_session_path do |f| %>
<%= f.error_messages %>
<span class="field-group">
<div>
<%= f.label :login, "Login Name:" %><br />
<%= f.text_field :login %><br />
</div>
</span>
<div class="clear"></div>
<span class="field-group">
<div>
<%= f.label :password, "Password:" %><br />
<%= f.password_field :password %> <span class="hint">Reminder: Your password is case sensitive.</span><br />
</div>
</span>
<div class="clear"></div>
<span class="field-group">
<div>
<%= f.check_box :remember_me %> <%= f.label :remember_me %>
</div>
</span>
<div class="clear"></div>
<%= f.submit "Login" %>
<% end %>
以及以下路由:
$ rake routes | grep user_session | grep show
user_session GET /user_session/:id(.:format) {:action=>"show", :controller=>"user_session"}
以及以下路由配置:
# user session stuff
resources :user_session do
member do
put :forgot_password
put :terms
get :terms
end
end
match '/login', :to => 'user_sessions#new', :as => 'login'
match '/logout', :to => 'user_sessions#destroy', :as => 'logout'
问题:
当我调用使用此部分的页面时,出现以下错误:
ActionController::RoutingError in User_sessions#new
Showing /app/views/edit_shared/_login.html.erb where line #2 raised:
No route matches {:action=>"show", :controller=>"user_session"}
已提取来源(围绕第2行):
1:
2: <%= form_for @user_session, :url => user_session_path do |f| %>
3: <%= f.error_messages %>
4: <span class="field-group">
5: <div>
所以该路线存在,但rails说它不存在。那么什么给出呢?直到我开始升级到 Rails 3.0.5 之前一切都很好。
Given:
The following partial:
<%= form_for @user_session, :url => user_session_path do |f| %>
<%= f.error_messages %>
<span class="field-group">
<div>
<%= f.label :login, "Login Name:" %><br />
<%= f.text_field :login %><br />
</div>
</span>
<div class="clear"></div>
<span class="field-group">
<div>
<%= f.label :password, "Password:" %><br />
<%= f.password_field :password %> <span class="hint">Reminder: Your password is case sensitive.</span><br />
</div>
</span>
<div class="clear"></div>
<span class="field-group">
<div>
<%= f.check_box :remember_me %> <%= f.label :remember_me %>
</div>
</span>
<div class="clear"></div>
<%= f.submit "Login" %>
<% end %>
And the following route:
$ rake routes | grep user_session | grep show
user_session GET /user_session/:id(.:format) {:action=>"show", :controller=>"user_session"}
And the following route configuration:
# user session stuff
resources :user_session do
member do
put :forgot_password
put :terms
get :terms
end
end
match '/login', :to => 'user_sessions#new', :as => 'login'
match '/logout', :to => 'user_sessions#destroy', :as => 'logout'
Problem:
When I call the page that uses this partial, I get the following error:
ActionController::RoutingError in User_sessions#new
Showing /app/views/edit_shared/_login.html.erb where line #2 raised:
No route matches {:action=>"show", :controller=>"user_session"}
Extracted source (around line #2):
1:
2: <%= form_for @user_session, :url => user_session_path do |f| %>
3: <%= f.error_messages %>
4: <span class="field-group">
5: <div>
So that route exists, but rails says it doesn't. So what gives? It was fine until I started upgrading to Rails 3.0.5.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试删除
:url =>表单中的 user_session_path
。通过指定form_for @user_session
,Rails 将查找user_session
RESTful 路由并为您生成 URL。Try removing the
:url => user_session_path
from your form. By specifyingform_for @user_session
, Rails will look up theuser_session
RESTful routes and generate the URL for you.更改
:url => user_session_path
到:url => {:动作=> :create}
成功了。changing
:url => user_session_path
to:url => {:action => :create}
did the trick.您的路线仅接受
GET
请求。表单默认发送POST
请求。编辑:
首先,您应该使用命名路由而不是
user_session_path
:并且您想要的操作是
create
,而不是new
(显示表单):Your route only accepts
GET
requests. A form sends aPOST
request by default.Edit:
First you should use your named route instead of
user_session_path
:And the action you want is
create
, notnew
(which displays the form):