简体   繁体   English

Ruby on Rails教程授权

[英]ruby on rails tutorial authorization

I'm working through the latest version of Michael Hartl's tutorial, and I have the following message: 我正在研究Michael Hartl教程的最新版本,并且收到以下消息:

Failures: 失败:

1) UserPages edit with valid information Failure/Error: sign_in user NoMethodError: undefined method sign_in' for #<RSpec::Core::ExampleGroup::Nested_4::Nested_4::Nested_3:0x9f1b1bc> # ./spec/requests/user_pages_spec.rb:66:in block (3 levels) in ' 1)使用有效信息编辑用户页面失败/错误:sign_in用户NoMethodError: sign_in' for #<RSpec::Core::ExampleGroup::Nested_4::Nested_4::Nested_3:0x9f1b1bc> # ./spec/requests/user_pages_spec.rb:66:in /spec/requests/user_pages_spec的未定义方法sign_in' for #<RSpec::Core::ExampleGroup::Nested_4::Nested_4::Nested_3:0x9f1b1bc> # ./spec/requests/user_pages_spec.rb:66:in在'

The same message nine times. 同一条消息九次。

I ran green until "Updating users". 我一直绿色运行,直到“更新用户”为止。

In the 9.2 I have changed the following programs as they were mention in the tutorial. 在9.2版中,我更改了本教程中提到的以下程序。 I was trying and trying for more than 10 hours and finally I gave up. 我尝试了十多个小时,最后我放弃了。 Would you please, help me. 请你帮我一下。

Juan Carrillo 胡安·卡里略(Juan Carrillo)

USERS.CONTROLLER.RB

class UsersController < ApplicationController

  before_filter :signed_in_user, only: [:edit, :update]

  def new
      @user = User.new
  end

  def show
    @user = User.find(params[:id])
  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 show
    @user = User.find(params[:id])
  end

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

  def update
     @user = User.find(params[:id])
    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
    redirect_to signin_url, notice: "Please sign in." unless signed_in?
  end
end


AUTHENTICATION_PAGES_SPEC.RB

require 'spec_helper' 需要'spec_helper'

describe "Authentication" do 描述“身份验证”

subject { page } 主题{页面}

describe "signin page" do before { visit signin_path } 在{访问signin_path}之前描述“登录页面”

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

describe "with valid information" do
  let(:user){ FactoryGirl.create(:user) }
  before do
    fill_in "Email", with: user.email.upcase
    fill_in "Password", with: user.password
    click_button"Sign in"
  end
  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

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

end 结束

describe "authorization" do

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

  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

end end 结束

USER_PAGES_SPEC.RB USER_PAGES_SPEC.RB

require 'spec_helper' 需要'spec_helper'

describe "UserPages" do subject { page } 描述“ UserPages”做主题{页面}

 describe "profile page" do

    let(:user) { FactoryGirl.create(:user) }
    before { visit user_path(user) }

    it { should have_selector('h1', text: user.name) }

    it { should have_selector('title', text: user.name) }

 end

describe "signup page" do
    before { visit signup_path }
    it { should have_selector('h1', text: 'Sign Up') }
    it { should have_selector('title', text: full_title('Sign Up')) }
end

describe "signup" do
    before { visit signup_path }
    let(:submit) { "Create my account" }

    describe "with invalid information" do
        it "should not create a user" do
            expect { click_button submit }.not_to change(User, :count)
        end

        describe "after submission" do
            before { click_button submit }

            it { should have_selector('title', text: 'Sign Up') }
            it { should have_content('error') }
        end
    end

    describe "with valid information" do
        before do
            fill_in "Name", with: "Example User"
            fill_in "Email", with: "user@example.com"
            fill_in "Password", with: "foobar"
            fill_in "Confirmation", with: "foobar"
        end
        it "should create a user" do
            expect { click_button submit }.to change(User, :count).by(1)
        end

        describe "after saving the user" do
            before { click_button submit }
            let(:user){ User.find_by_email('user@example.com') }
            it { should have_selector('title', text: user.name) }
            it { should have_selector('div.alert.alert-success', text: 'Welcome') }

            it { should have_link('Sign out') }

        end
    end

end 结束

  describe "edit" do
    let(:user) { FactoryGirl.create(:user) }
    before do
        sign_in user
        visit edit_user_path(user)
    end

    describe "page" do
      it { should have_selector('h1',    text: "Update your profile") }
      it { should have_selector('title', text: "Edit user") }
      it { should have_link('change', href: 'http://gravatar.com/emails') }
    end

    describe "with invalid information" do
      before { click_button "Save changes" }

      it { should have_content('error') }
    end

    describe "with valid information" do
        let(:new_name) { "New Name" }
        let(:new_email) { "new@example.com" }
        before do
            fill_in "Name", with: new_name
            fill_in "Email", with: new_email
            fill_in "Password", with: user.password
            fill_in "Confirm Password", with: user.password
            click_button "Save changes"
        end
        it { should have_selector('title', text: new_name) }
        it { should have_selector('div.alert.alert-success') }
        it { should have_link('Sign out', href:signout_path) }
        specify { user.reload.name.should == new_name }
        specify { user.reload.email.should == new_email }
    end

end 结束

end 结束

This is telling your that the sign_in method has not been defined. 这表明您尚未定义sign_in方法。 You need to add in in utilities.rb as per 9.6 in the tutorial 您需要在加入utilities.rb按9.6教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM