Ruby on Rails教程9.2.3友好的转发错误

时间:2022-07-18 00:13:43

I am very new to programming and I am trying to go over this tutorial. I have followed the codes given with the exception of writing my own code for the exercises at the end of the chapter. I am now stuck on this error.

我对编程非常陌生,我正在尝试浏览本教程。除了为本章末尾的练习编写我自己的代码之外,我已经遵循了所给出的代码。我现在被这个错误困住了。

Failures:

  1) Authentication authorization for non-signed-in users when attempting to visit a protected page after signing in should render the desired protected page
 Failure/Error: page.should have_selector('title', text: 'Edit user')
   expected css "title" with text "Edit user" to return something
 # ./spec/requests/authentication_pages_spec.rb:63:in `block (6 levels) in <top (required)>'

Finished in 3.12 seconds
73 examples, 1 failure

Failed examples:

rspec ./spec/requests/authentication_pages_spec.rb:62 # Authentication authorization for non-signed-in users when attempting to visit a protected page after signing in should render the desired protected page

Here are the code that I added.

这是我添加的代码。

spec/requests/authentication_pages_spec.rb

规范/要求/ authentication_pages_spec.rb

require 'spec_helper'

describe "Authentication" do

  subject { page }

  describe "signin page" do
    before { visit signin_path }

    it { should have_selector('h1',    text: 'Sign in') }
    it { should have_selector('title', text: 'Sign in') }
  end

  describe "signin" do

    before { visit signin_path }

    describe "with invalid information" do
      before { click_button "Sign in" }

      it { should have_selector('title', text: 'Sign in') }
      it { should have_selector('div.alert.alert-error', text: 'Invalid') }

      describe "after visiting another page" do
        before { click_link "Home" }
        it { should_not have_selector('div.alert.alert-error') }
      end
    end

    describe "with valid information" do
      let(:user) { FactoryGirl.create(:user) }
      before { sign_in user }

      it { should have_selector('title', text: user.name) }
      it { should have_link('Profile',  href: user_path(user)) }
      it { should have_link('Settings', href: edit_user_path(user)) }
      it { should have_link('Sign out', href: signout_path) }
      it { should_not have_link('Sign in', href: signin_path) }

      describe "followed by signout" do
        before { click_link "Sign out" }
        it { should have_link('Sign in') }
      end
    end
  end

  describe "authorization" do

    describe "for non-signed-in users" do
      let(:user) { FactoryGirl.create(:user) }

      describe "when attempting to visit a protected page" do
        before do
          visit edit_user_path(user)
          fill_in "Email",    with: user.email
          fill_in "Password", with: user.password
          click_button "Sign in"
        end

        describe "after signing in" do

          it "should render the desired protected page" do
            page.should have_selector('title', text: 'Edit user')
          end
        end
      end

      describe "in the Users controller" do

        describe "visiting the edit page" do
          before { visit edit_user_path(user) }
          it { should have_selector('title', text: 'Sign in') }
        end

        describe "submitting to the update action" do
          before { put user_path(user) }
          specify { response.should redirect_to(signin_path) }
        end
      end
    end

    describe "as wrong user" do
      let(:user) { FactoryGirl.create(:user) }
      let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@example.com") }
      before { sign_in user }

      describe "visiting Users#edit page" do
        before { visit edit_user_path(wrong_user) }
        it { should_not have_selector('title', text: full_title('Edit user')) }
      end

      describe "submitting a PUT request to the Users#update action" do
        before { put user_path(wrong_user) }
        specify { response.should redirect_to(root_path) }
      end
    end
  end
end

app/helpers/sessions_helper.rb

应用程序/帮助/ sessions_helper.rb

module SessionsHelper

  def sign_in(user)
    cookies.permanent[:remember_token] = user.remember_token
    self.current_user = user
  end

  def signed_in?
    !current_user.nil?
  end

  def current_user=(user)
    @current_user = user
  end

  def current_user
    @current_user ||= User.find_by_remember_token(cookies[:remember_token])
  end

  def current_user?(user)
    user == current_user
  end

  def sign_out
    self.current_user = nil
    cookies.delete(:remember_token)
  end

  def redirect_back_or(default)
    redirect_to(session[:return_to] || default)
    session.delete(:return_to)
  end

  def store_location
    session[:return_to] = request.url
  end
end

app/controllers/users_controller.rb

app / controllers / users_controller.rb

class UsersController < ApplicationController
  before_filter :signed_in_user, only: [:edit, :update]
  before_filter :correct_user,   only: [:edit, :update]

  def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      sign_in @user
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end

  def edit
  end

  def update
    if @user.update_attributes(params[:user])
      flash[:success] = "Profile updated"
      sign_in @user
      redirect_to @user
    else
      render 'edit'
    end
  end

  private

    def signed_in_user
      unless signed_in?
        store_location
        redirect_to signin_url, notice: "Please sign in."
      end
    end

    def correct_user
      @user = User.find(params[:id])
      redirect_to(root_path) unless current_user?(@user)
    end
end

This is my code after doing the 1st exercise on Chapter 8. app/controllers/sessions_controller.rb

这是我在第八章第一次练习后的代码。app / controllers / sessions_controller.rb

class SessionsController < ApplicationController

  def new
  end

  def create
    user = User.find_by_email(params[:email])
    if user && user.authenticate(params[:password])
        session[:user] = user.id
        sign_in user
        redirect_to user
    else
        flash.now[:error] = 'Invalid email/password combination'
        render 'new'
    end 
  end

  def destroy
    sign_out
     redirect_to root_path
  end
end

1 个解决方案

#1


4  

I found my problem and solution. The following code in sessions_controller.rb needs a change.

我找到了我的问题和解决方案。sessions_controller中的以下代码。rb需要改变。

  def create
    user = User.find_by_email(params[:email])
    if user && user.authenticate(params[:password])
        session[:user] = user.id
        sign_in user
        redirect_to user #<--- this line is wrong
    else
        flash.now[:error] = 'Invalid email/password combination'
        render 'new'
    end 
  end

it should be

它应该是

redirect_back_or user

#1


4  

I found my problem and solution. The following code in sessions_controller.rb needs a change.

我找到了我的问题和解决方案。sessions_controller中的以下代码。rb需要改变。

  def create
    user = User.find_by_email(params[:email])
    if user && user.authenticate(params[:password])
        session[:user] = user.id
        sign_in user
        redirect_to user #<--- this line is wrong
    else
        flash.now[:error] = 'Invalid email/password combination'
        render 'new'
    end 
  end

it should be

它应该是

redirect_back_or user