无法使用Authlogic路由/注销

时间:2015-08-30 23:50:23

标签: ruby-on-rails-4 authlogic

我一直在尝试使用Authlogic在我的应用中设置用户身份验证,并且我确定问题出现在我的代码中,但我似乎无法找到它。当我点击/ login链接时,一切都按预期工作,但我/ logout想要使用GET而不是DELETE。

routing.db

Rails.application.routes.draw do

  root 'comments#index'

  resources :roles
  resources :subjects
  resources :comments
  resources :topics
  resources :users
  resources :user_sessions, only: [:create, :destroy]

  delete '/logout', to: 'user_sessions#destroy', as: :logout
  get '/login', to: 'user_sessions#new', as: :login
end

user_sessions_controller

class UserSessionsController < ApplicationController

  before_filter :require_no_user, :only => [:new, :create]
  before_filter :require_user, :only => :destroy

  def new
    @user_session = UserSession.new
  end

  def create
    @user_session = UserSession.new(user_session_params)
    if @user_session.save
        flash[:success] = "Welcome back!"
        redirect_to root_path
    else
        render :action => :new
    end
  end

  def destroy
    current_user_session.destroy
    flash[:success] = "Goodbye!"
    redirect_to root_path
  end

  private

  def user_session_params
    params.require(:user_session).permit(:username, :password, :remember_me)
  end
end

application.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title><%= content_for?(:title) ? yield(:title) : "Untitled" %></title>
    <%= stylesheet_link_tag "application" %>
    <%= javascript_include_tag :defaults %>
    <%= csrf_meta_tag %>
    <%= yield(:head) %>
  </head>
  <body>
    <div id="user_nav">
        <% if current_user %>
            <span><%= current_user.username %></span>
            <%= link_to "Edit profile", edit_user_path(:current) %>
            <%= link_to 'Sign Out', logout_path, :method => :delete %>
        <% else %>
            <%= link_to "Register", new_user_path %>
            <%= link_to 'Sign In', login_path %>
        <% end %>
    </div>
    <div id="container">
      <% flash.each do |name, msg| %>
        <%= content_tag :div, msg, :id => "flash_#{name}" %>
      <% end %>
      <%= content_tag :h1, yield(:title) if show_title? %>
      <%= yield %>
    </div>
  </body>
</html>

这里有什么问题吗?这是我得到的错误:

Start of error page

Relevant routes on error page

1 个答案:

答案 0 :(得分:1)

application.html.erb文件中:

变化:

<%= javascript_include_tag :defaults %>

要:

<%= javascript_include_tag "application" %>

之后它应该工作!