所以我下面的tutorial由邁克爾·哈特爾,當我試圖註銷logout_url紅寶石,我得到這個 "NameError in SessionsController#destroy"undefined local variable or method "log_out" for #<SessionsController:0x0000000275f290> Did you mean? logout_url
nameerror未定義的局部變量或方法`log_out'您的意思是?在軌道上
我的測試中,該錯誤只彈出後,我在我的測試中添加的最後一位但是,我在網站上的註銷功能也不起作用。
這是我session_helper.rb:
module SessionsHelper
# Logs in the given user.
def log_in(user)
session[:user_id] = user.id
end
# Returns the current logged-in user (if any).
def current_user
@current_user ||= User.find_by(id: session[:user_id])
end
# Returns true if the user is logged in, false otherwise.
def logged_in?
!current_user.nil?
end
# Logs out the current user.
def destroy
session.delete(:user_id)
@current_user = nil
end
end
這是我session_controller:
class SessionsController < ApplicationController
def new
end
def create
#Gets user from database in lowercase & determines if user is valid
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
# Log the user in and redirect to the user's show page.
log_in user
redirect_to user
else
# Create an error message.
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
end
end
# Logs out the current user.
def destroy
log_out # undefined variable Name error
redirect_to root_url
end
end
我application_controller:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
include SessionsHelper #temporary session cookie, expires automatically upon browser close
end
我的看法/佈局/ _header.html.erb (用於註銷鏈接的導航欄):
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="container">
<%= link_to image_tag("logo2.png", alt: "CourseBuddies logo"), root_path, id: "logo" %>
<%- # link_to "sample app", '#', id: "logo" %>
<nav>
<ul class="nav navbar-nav navbar-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Reviews", '#' %></li>
<li><%= link_to "About us", about_path %></li>
<%- # LOGIN & SCROLL DOWN BAR %>
<% if logged_in? %>
**<li><%= link_to "Users", users_path %></li>
<li class="dropdown">**
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account <b class="caret"></b>
</a>
<ul class="dropdown-menu">
**<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", '#' %></li>**
<li class="divider"></li>
<li>
**<%= link_to "Log out", logout_path, method: :delete %>**
</li>
</ul>
</li>
**<% else %>
<li><%= link_to "Log in", login_path %></li>
<% end %>**
</ul>
</nav>
</div>
</header>
我的用戶模型:
class User < ApplicationRecord
#before saving, make sure the email is in downcase
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: {case_sensitive: false}
# Let's you safely store a hashed password_digest to DB,
# gives you password & password_confirmation attributes,
# an authenticate method that returns the user when pw is correct,
# otherwise false.
has_secure_password(validations:false)
validates :password, presence:true, length: { minimum: 6 }
# Returns the hash digest of the given string.
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
end
我在我的用戶控制器創建-方法: DEF創建 @user = User.new(user_params)#params [:用戶])#user_params)
if @user.save
log_in @user
flash[:success] = "Welcome to CourseBuddies!"
redirect_to @user
else
render 'new'
end
末
我的測試/ test_helper:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/reporters"
Minitest::Reporters.use!
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Returns true if a test user is logged in.
def is_logged_in?
!session[:user_id].nil?
end
end
而且我user_login_test:
require 'test_helper'
class UsersLoginTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
end
# Visit the login path.
# Verify that the new sessions form renders properly.
# Post to the sessions path with an invalid params hash.
# Verify that the new sessions form gets re-rendered and that a flash message appears.
# Visit another page (such as the Home page).
# Verify that the flash message doesn’t appear on the new page.
test "login with valid information followed by logout" do
get login_path
post login_path, params: { session: { email: @user.email,
password: 'password' } }
assert is_logged_in?
assert_redirected_to @user
follow_redirect!
assert_template 'users/show'
assert_select "a[href=?]", login_path, count: 0
assert_select "a[href=?]", logout_path
assert_select "a[href=?]", user_path(@user)
#AFTER ADDING THIS MY TEST FAILED
#after logging in, we use delete to issue a DELETE request to the logout path
#(Table 8.1) and verify that the user is logged out and redirected to the root URL
delete logout_path
assert_not is_logged_in?
assert_redirected_to root_url
follow_redirect!
assert_select "a[href=?]", login_path
assert_select "a[href=?]", logout_path, count: 0
assert_select "a[href=?]", user_path(@user), count: 0
end
end
最後
我的路線:
Rails.application.routes.draw do
get 'sessions/new'
#ROOT, first page to show
root 'pages#home'
# maps requests for the URL/pages/home to the about us action in the Pages controller.
# By using GET we arrange for the route to respond to a GET request.
# With this we generate a about us action inside the Pages controller, automatically
# get a page at the address /pages/about us
get '/about', to: 'pages#about'
get '/signup', to: 'users#new'
post '/signup', to: 'users#create' #signup route that responds to POST requests.
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
#resources :sessions
resources :users
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
對不起有很多的代碼!在教程中,它表示在添加新代碼後,測試users_login_test應該變爲綠色,但失敗。此外,註銷不起作用,並給我這個錯誤: image of error
我將不勝感激任何幫助!
謝謝您注意到我的愚蠢錯誤並再次教育我,Pavan!你是最好的 ! – Myth