Michael Hartl教程3.2 - 第9章 - 错误消息和编辑/更新页面问题

时间:2012-08-08 16:26:26

标签: ruby-on-rails ruby

我正在研究Michael Hartl Rails 3.2,我已经坚持了几个小时这些问题。我无法弄清楚我错过了什么,因为我已经完成了7,8,9中的所有代码,以确保它是准确的。更新/编辑页面一直有效,直到我添加到用户控件的私有部分。

由于我添加了这些内容,当我手动测试页面并点击设置时,它会直接进入登录页面。除非signed_in,它似乎没有取消?并从用户控制器页面将其重定向到签名页面。我的测试失败是有道理的,因为编辑页面没有登录。任何想法或建议都将非常感激。如果您需要查看其他页面,请告诉我们。

错误消息 - 我得到9个错误,如下所示

Failures:

1) UserPages edit page 
 Failure/Error: it { should have_selector('h1', text: "Update your profile") }
   expected css "h1" with text "Update your profile" to return something
 # ./spec/requests/user_pages_spec.rb:73:in `block (4 levels) in <top (required)>'

2) UserPages edit page 
 Failure/Error: it { should have_link('change', href: 'http://gravatar.com/emails')}
   expected link "change" to return something
 # ./spec/requests/user_pages_spec.rb:75:in `block (4 levels) in <top (required)>'

3) UserPages edit page 
 Failure/Error: it { should have_selector('title', text: "Edit user") }
   expected css "title" with text "Edit user" to return something
 # ./spec/requests/user_pages_spec.rb:74:in `block (4 levels) in <top (required)>'

4) UserPages edit with valid information 
 Failure/Error: fill_in "Name", with: new_name
 Capybara::ElementNotFound:
   cannot fill in, no text field, text area or password field with id, name, or label 'Name' found
 # (eval):2:in `fill_in'
 # ./spec/requests/user_pages_spec.rb:88:in `block (4 levels) in <top (required)>'

5) UserPages edit with valid information 
 Failure/Error: fill_in "Name", with: new_name
 Capybara::ElementNotFound:
   cannot fill in, no text field, text area or password field with id, name, or label 'Name' found
 # (eval):2:in `fill_in'
 # ./spec/requests/user_pages_spec.rb:88:in `block (4 levels) in <top (required)>'

6) UserPages edit with valid information 
 Failure/Error: fill_in "Name", with: new_name
 Capybara::ElementNotFound:
   cannot fill in, no text field, text area or password field with id, name, or label 'Name' found
 # (eval):2:in `fill_in'
 # ./spec/requests/user_pages_spec.rb:88:in `block (4 levels) in <top (required)>'

7) UserPages edit with valid information 
 Failure/Error: fill_in "Name", with: new_name
 Capybara::ElementNotFound:
   cannot fill in, no text field, text area or password field with id, name, or label 'Name' found
 # (eval):2:in `fill_in'
 # ./spec/requests/user_pages_spec.rb:88:in `block (4 levels) in <top (required)>'

8) UserPages edit with valid information 
 Failure/Error: fill_in "Name", with: new_name
 Capybara::ElementNotFound:
   cannot fill in, no text field, text area or password field with id, name, or label 'Name' found
 # (eval):2:in `fill_in'
 # ./spec/requests/user_pages_spec.rb:88:in `block (4 levels) in <top (required)>'

9) UserPages edit with invalid information 
 Failure/Error: before { click_button "Save changes" }
 Capybara::ElementNotFound:
   no button with value or id or text 'Save changes' found
 # (eval):2:in `click_button'
 # ./spec/requests/user_pages_spec.rb:79:in `block (4 levels) in <top (required)>'

以下是身份验证规范页

require 'spec_helper'

describe "AuthenticationPages" 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 }


        describe "followed by signout" do
            before { click_link "Sign out" }
            it { should have_link('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) }

    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

用户页面规范

require 'spec_helper'

describe "UserPages" do

subject { page }

describe "profile page" do
    before { visit user_path(user) }
    let(:user) {FactoryGirl.create(: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: "Exmaple User"
            fill_in "Email", with: "user@example.com"
            fill_in "Password", with: "foobar"
            fill_in "Password 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
  visit signin_path
  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

来自Utilities的sign_in(用户)

def sign_in(user)
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
# Sign in when not using Capybara as well.
cookies[:remember_token] = user.remember_token

Sessions Helper

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.fullpath
end
end     

用户控制器

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

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

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

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

end

视图/布局/ _header.html.erb

<header class="navbar navbar-fixed-top">
    <div class="navbar-inner">
        <div class="container">
            <%= link_to "sample app", root_path, id: "logo" %>
            <nav>
                <ul class="nav pull-right">
                    <li><%= link_to "Home",  root_path %></li>
                    <li><%= link_to "Help",  help_path %></li>
                    <% if signed_in? %>
                        <li><%= link_to "Users", "#" %></li>
                        <li id="fat-menu" 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", edit_user_path(current_user) %></li>    
                                <li class="divider"></li>
                                <li>
                                    <%= link_to "Sign out", signout_path, method: "delete" %>
                                </li>
                            </ul>
                        </li>
                    <% else %>
                        <li><%= link_to "Sign in", signin_path %></li>
                    <% end %>
                </ul>
            </nav>
        </div>
    </div>

如果遇到此问题 - 请确保在application_controller中没有此问题。这是一个非常小的错误,给我带来了各种各样的悲伤。再次 - 请勿使用此代码......

class ApplicationController < ActionController::Base
 protect_from_forgery
 include SessionsHelper

 private
def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
end

1 个答案:

答案 0 :(得分:1)

Capybara找不到电子邮件地址。

在填写登录表单之前,您应该继续操作。改变你的功能:

def sign_in(user)
  visit signin_path
  fill_in "Email", with: user.email
  fill_in "Password", with: user.password
  click_button "Sign in"
  # Sign in when not using Capybara as well.
  cookies[:remember_token] = user.remember_token
end

我认为你的表格形式正确。您可以在visit signin_path之后添加puts page.html以查看html,如果安装了launchy,则可以添加save_and_open_page