2012-10-03 122 views
2

刚刚完成Michael Hartl的Ruby on Rails教程Section 9.3 - Unsuccessful Edits。我一直想花几个小时来弄清楚为什么我无法通过检查来通过检查不成功的编辑。Ruby on Rails教程 - 第9章:缺少模板更新

这里是我加了一章的代码:

规格/请求/ 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_error_message('Invalid') } 

     describe "after visiting another page" do 
     before { click_link "Home" } 
     it { should_not have_error_message } 
     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 
end 

规格/请求/ user_pages_spec.rb:

require 'spec_helper' 

describe "User pages" do 

    subject { page } 

    describe "signup page" do 
    before { visit signup_path } 

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

    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" 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("Name can't be blank") } 
     it { should have_content("Email can't be blank") } 
     it { should have_content("Email is invalid") } 
     it { should have_content("Password can't be blank") } 
     it { should have_content("Password is too short") } 
     it { should have_content("Password confirmation can't be blank") } 
     end 
    end 

    describe "with valid information" do 
     before do 
     fill_in "Name",   with: "Example User" 
     fill_in "Email",  with: "[email protected]" 
     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('[email protected]') } 
     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 { visit edit_user_path(user) } 

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

spec/support/utilities.rb:

include ApplicationHelper 

def full_title(page_title) 
    base_title = "Ruby on Rails Tutorial Sample App" 
    if page_title.empty? 
    base_title 
    else 
    "#{base_title} | #{page_title}" 
    end 
end 

def valid_signin(user) 
    fill_in "Email", with: user.email 
    fill_in "Password", with: user.password 
    click_button "Sign in" 
end 

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 

RSpec::Matchers.define :have_error_message do |message| 
    match do |page| 
    page.should have_selector('div.alert.alert-error', text: message) 
    end 
end 

应用程序/控制器/ users_controllers.rb:

class UsersController < ApplicationController 

    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]) 
     # Handle a successful update. 
    else 
     render 'edit' 
    end 
    end 
end 

应用/模型/ users.rb的

class User < ActiveRecord::Base 
    attr_accessible :email, :name, :password, :password_confirmation 
    has_secure_password 

    before_save { self.email.downcase! } 
    before_save :create_remember_token 

    # Validate that a name is not blank and is no longer than50 characters 
    validates :name, presence: true, length: { maximum: 50 } 

    # Validate that an email address is not blank, contains a valid pattern, and 
    # is not already in the database 
    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    validates :email, presence: true, 
        format: { with: VALID_EMAIL_REGEX }, 
        uniqueness: { case_sensitive: false } 

    #Validate the password is there and is at least 6 characters 
    validates :password, length: { minimum: 6 }, on: :create 
    validates :password_confirmation, presence: true, on: :create 

    # Everything below this private is only visibile to this user model 
    private 

    # Creates a remember token for the user (not a local variable) 
    # Used to keep a user login active indefinitely after sign in 
    def create_remember_token 
     self.remember_token = SecureRandom.urlsafe_base64 
    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> 
</header> 

应用程序/视图/用户/ edit.html.erb:

<% provide(:title, "Edit user") %> 
<h1>Update your profile</h1> 

<div class="row"> 
    <div class="span6 offset3"> 
    <%= form_for(@user) do |f| %> 
     <%= render 'shared/error_messages' %> 

     <%= f.label :name %> 
     <%= f.text_field :name %> 

     <%= f.label :email %> 
     <%= f.text_field :email %> 

     <%= f.label :password %> 
     <%= f.password_field :password %> 

     <%= f.label :password_confirmation, "Confirm Password" %> 
     <%= f.password_field :password_confirmation %> 

     <%= f.submit "Save changes", class: "btn btn-large btn-primary" %> 
    <% end %> 

    <%= gravatar_for @user %> 
    <a href="http://gravatar.com/emails">change</a> 
    </div> 
</div> 

我收到以下错误:

Failures: 

    1) User pages edit with invalid information 
    Failure/Error: before { click_button "Save changes" } 
    ActionView::MissingTemplate: 
     Missing template users/update, application/update with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: 
     * "/home/cbachich/Development/rails_projects/sample_app/app/views" 
    # (eval):2:in `click_button' 
    # ./spec/requests/user_pages_spec.rb:79:in `block (4 levels) in <top (required)>' 

Finished in 2.65 seconds 
67 examples, 1 failure 

Failed examples: 

rspec ./spec/requests/user_pages_spec.rb:81 # User pages edit with invalid information 

回答

10

在成功更新用户的情况下,您的控制器不会执行任何操作,这意味着Rails将呈现用户/更新视图。既然你没有这个观点,那就是你看到的错误。 Saverio的笔记很好 - 你应该添加一个redirect_to去看演出。这将摆脱这个错误。

但还有一个更大的问题。当你期望它采取失败路径并呈现编辑视图时,为什么你的控制器会采取成功的更新路径?

根据我的经验,编辑页面填写电子邮件和电子邮件,但将密码留空。您希望更新失败,因为没有密码,但更新成功。为什么?如果你看模型,你的密码验证只有on: :create。这意味着这些验证不会在更新上执行。您期望在更新上执行密码验证,但它们不是,这就是编辑成功而不是失败的原因。

如果您更新模型以对创建和更新操作执行验证(可能类似于on: { :create, :update }),则应重新开始工作。无效数据的更新将会失败。

+0

这实际上听起来像它可能是我的问题。当我回家时,我会在今晚测试它。 – CaptCheech

+0

这工作。我想默认是在创建和更新,所以我刚刚从用户模型中移除,并通过测试。感谢凯尔! – CaptCheech

1

你有没有为UsersController的更新模板行动?

它应该在app/view/users/update.html.erb

在旁注中,通常一个会在成功更新时结束重定向,因此如果在更新操作中添加了适当的redirect_to,则可能不需要该模板。

+0

我没有更新模板,但我不认为我需要这个测试。我没有达到测试成功编辑的程度。所以基于用户控制器,它“应该”呈现一个编辑。或者至少我是这样解释代码的。 – CaptCheech

+0

错误特别抱怨该模板丢失。是什么让你认为事实并非如此? – rewritten

+0

失败的测试是假设检查提交的无效数据。所以它不应该指向更新页面,它应该被重定向到编辑页面,因为它不应该通过认证。我同意这个错误特别是它缺少模板,但是这个测试不应该关心更新模板。至少还没有。 – CaptCheech

0

以下块

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

     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 
    end 

创建@user和没有在形式改变任何点击保存按钮,因此它不发送所有必需的字段内容,它是在相应的成功更新处理控制器。

如果您更改与

describe "with invalid information" do 
    before do 
     fill_in "Name", with: " " 
     click_button "Save changes" 
    end 

    it { should have_content('error') } 
    end 

最后一块,然后名称字段为空,该操作将在else块渲染“编辑”不成功的更新和秋天来处理。