Rails 升级:服务器说路由不存在,而实际上它存在!

发布于 2024-11-05 21:16:18 字数 2057 浏览 5 评论 0原文

给出:

以下部分:

<%= 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰葑 2024-11-12 21:16:18

尝试删除 :url =>表单中的 user_session_path。通过指定 form_for @user_session,Rails 将查找 user_session RESTful 路由并为您生成 URL。

Try removing the :url => user_session_path from your form. By specifying form_for @user_session, Rails will look up the user_session RESTful routes and generate the URL for you.

想念有你 2024-11-12 21:16:18

更改 :url => user_session_path:url => {:动作=> :create} 成功了。

changing :url => user_session_path to :url => {:action => :create} did the trick.

暮年 2024-11-12 21:16:18

您的路线仅接受 GET 请求。表单默认发送 POST 请求。

编辑:
首先,您应该使用命名路由而不是 user_session_path

<%= form_for @user_session, :url => login_path do |f| %>

并且您想要的操作是 create,而不是 new(显示表单):

match '/login', :to => 'user_sessions#create', :as => 'login'

Your route only accepts GET requests. A form sends a POST request by default.

Edit:
First you should use your named route instead of user_session_path:

<%= form_for @user_session, :url => login_path do |f| %>

And the action you want is create, not new (which displays the form):

match '/login', :to => 'user_sessions#create', :as => 'login'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文