ActionController::RoutingError(没有路由匹配“/user_sessions/……”)

发布于 2025-01-07 22:29:51 字数 1822 浏览 2 评论 0原文

我对 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 技术交流群。

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

发布评论

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

评论(2

揪着可爱 2025-01-14 22:29:51

好吧,首先,你有一个糟糕的路线:

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

注意 new 而不是 destroy

另外,在更高版本中不需要 to_key - 我正在使用 Rails 3 并且不这样做我的 UserSession 模型中没有它。

Ok, first, you have a bad route:

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

note the new instead of destroy

also, the to_key is not needed in later versions - I'm using rails 3 and don't have it in my UserSession Model.

小霸王臭丫头 2025-01-14 22:29:51

绝对需要更改您的路线以不匹配登录来销毁。
这是我的路线设置...(来自“Rails 敏捷 Web 开发”示例)。

controller :user_sessions do

  get 'login' => :new
  post 'login' => :create
  delete 'logout' => :destroy
end

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).

controller :user_sessions do

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