ActionController::RoutingError(没有路由匹配“/user_sessions/……”)
我对 Ruby on Rails 非常陌生。我正在尝试使用 Authlogic 制作一个身份验证系统(遵循此教程)。我收到的错误是在我提交登录表单后:
No route matches "/user_sessions/%23%3CUserSession:0x103486aa8%3E"
令人惊讶的是,提交表单后页面的 URL 也引发了错误:
http://localhost:3000/user_sessions/%23%3CUserSession:0x103486aa8%3E
我不知道我做错了什么以及那个奇怪的 UserSession 在哪里代码的东西是从来的!!!
这是我的登录表单的样子:
<% form_for @user_session do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :username %><br />
<%= f.text_field :username%>
</p>
<p>
<%= f.label :password %><br />
<%= f.password_field :password %>
</p>
<p><%= f.submit "Submit" %></p>
<% end %>
这是我的 UserSession 类:
class UserSession < Authlogic::Session::Base
def to_key
new_record? ? nil : [ self.send(self.class.primary_key) ]
end
end
以及我的 UserSessionController 的创建操作:
def create
@user_session = UserSession.new(params[:user_session])
if @user_session.save
flash[:notice] = "Login successful!"
redirect_back_or_default root_path
else
render :action => :new
end
end
ApplicationController 中的“redirect_back_or_default”方法:
def redirect_back_or_default(default)
redirect_to(session[:return_to] || default)
session[:return_to] = nil
end
最后,routes.rb 中与 user_sessions 相关的所有内容:
resources :user_sessions
match 'login' => "user_sessions#destroy", :as => :login
match 'logout' => "user_sessions#destroy", :as => :logout
我认为这些代码可能与出现该错误有关。如果我应该添加更多代码以使其更清晰,请告诉我。
I'm super new to Ruby on Rails. I'm trying to make an authentication system using Authlogic (following this tutorial). The error that I'm getting is right after I submit the login form:
No route matches "/user_sessions/%23%3CUserSession:0x103486aa8%3E"
Surprisingly the URL of the page right after the form is submitted which also brings up the error is:
http://localhost:3000/user_sessions/%23%3CUserSession:0x103486aa8%3E
I have no idea what I have done wrong and where that weird UserSession code thing is coming from!!!
This is how my login form looks like:
<% form_for @user_session do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :username %><br />
<%= f.text_field :username%>
</p>
<p>
<%= f.label :password %><br />
<%= f.password_field :password %>
</p>
<p><%= f.submit "Submit" %></p>
<% end %>
Here is my UserSession class:
class UserSession < Authlogic::Session::Base
def to_key
new_record? ? nil : [ self.send(self.class.primary_key) ]
end
end
and the create action of my UserSessionController:
def create
@user_session = UserSession.new(params[:user_session])
if @user_session.save
flash[:notice] = "Login successful!"
redirect_back_or_default root_path
else
render :action => :new
end
end
"redirect_back_or_default" method in ApplicationController:
def redirect_back_or_default(default)
redirect_to(session[:return_to] || default)
session[:return_to] = nil
end
And lastly everything related to user_sessions in routes.rb:
resources :user_sessions
match 'login' => "user_sessions#destroy", :as => :login
match 'logout' => "user_sessions#destroy", :as => :logout
These are the codes that I thought could be involved in getting that error. If I should add some more code to make it more clear please let me know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,首先,你有一个糟糕的路线:
注意
new
而不是destroy
另外,在更高版本中不需要 to_key - 我正在使用 Rails 3 并且不这样做我的 UserSession 模型中没有它。
Ok, first, you have a bad route:
note the
new
instead ofdestroy
also, the to_key is not needed in later versions - I'm using rails 3 and don't have it in my UserSession Model.
绝对需要更改您的路线以不匹配登录来销毁。
这是我的路线设置...(来自“Rails 敏捷 Web 开发”示例)。
Definitely need to change your route to not match login to destroy.
Here's the route setting I have... (from "Agile Web Development with Rails" example).