Ruby on rails 迈克尔·哈特尔';s Rails教程第8.1章Rspec错误

Ruby on rails 迈克尔·哈特尔';s Rails教程第8.1章Rspec错误,ruby-on-rails,rspec,ruby-on-rails-4,railstutorial.org,Ruby On Rails,Rspec,Ruby On Rails 4,Railstutorial.org,首先,我感谢大家抽出时间来审议我的问题 我是一名rails新手,正在学习Michael Hartl的rails教程,在运行我的Rspec时,我遇到了以下错误消息 1) Authentication with valid information Failure/Error: click_button "Sign in" ActionView::MissingTemplate: Missing template sessions/create, applicati

首先,我感谢大家抽出时间来审议我的问题

我是一名rails新手,正在学习Michael Hartl的rails教程,在运行我的Rspec时,我遇到了以下错误消息

1) Authentication with valid information 
     Failure/Error: click_button "Sign in"
     ActionView::MissingTemplate:
       Missing template sessions/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
         * "/Users/Ladarris/rails_projects/sample_app/app/views"
     # ./spec/requests/authentication_pages_spec.rb:24:in `block (3 levels) in <top (required)>'

  2) Authentication with valid information 
     Failure/Error: click_button "Sign in"
     ActionView::MissingTemplate:
       Missing template sessions/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
         * "/Users/Ladarris/rails_projects/sample_app/app/views"
     # ./spec/requests/authentication_pages_spec.rb:24:in `block (3 levels) in <top (required)>'

  3) Authentication with valid information 
     Failure/Error: click_button "Sign in"
     ActionView::MissingTemplate:
       Missing template sessions/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
         * "/Users/Ladarris/rails_projects/sample_app/app/views"
     # ./spec/requests/authentication_pages_spec.rb:24:in `block (3 levels) in <top (required)>'

  4) Authentication with valid information 
     Failure/Error: click_button "Sign in"
     ActionView::MissingTemplate:
       Missing template sessions/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
         * "/Users/Ladarris/rails_projects/sample_app/app/views"
     # ./spec/requests/authentication_pages_spec.rb:24:in `block (3 levels) in <top (required)>'

Finished in 0.71396 seconds
42 examples, 4 failures
下面是我的代码副本。 ___________________

Rspec试验

会话控制器

它正在查看
new.html.erb
,但当它到达该视图中的
f.submit
时,会生成另一个请求,并在会话控制器中执行
create
方法。但是,对于
if
分支(即当用户存在时),您没有任何代码,因此它会采取默认操作,尝试以
create
的名称呈现视图


如果仔细检查,我怀疑您会发现您尚未实现教程要求您在教程中实现的某些代码。

确保controller已实现If语句 -->app/controller/users\u controller.rb

def create
  @user = User.new(user_params)

  if @user.save
    redirect_to @user    #This statement is important
  else
    render 'new'
  end
end

@用户2788206,请查看本书的“8.2成功签约”部分,该部分提供了您问题的解决方案。
require 'spec_helper'

describe "Authentication" do
  subject { page }
  describe "signin page" do
    before { visit signin_path }
    it { should have_content('Sign in') }
    it { should have_title('Sign in') }
  end
  describe "signin" do
      before { visit signin_path }
      describe "with invalid information" do
        before { click_button "Sign in" }
        it { should have_title('Sign in') }
        it { should have_selector('div.alert.alert-error', text: 'Invalid') }
      end
  end
   describe "with valid information" do
          let(:user) { FactoryGirl.create(:user) }
          before do
            visit signin_path
            fill_in "Email",    with: user.email.upcase
            fill_in "Password", with: user.password
            click_button "Sign in"
          end
          it { should have_title(user.name) }
          it { should have_link('Profile',     href: user_path(user), visible: false) }
          it { should have_link('Sign out',    href: signout_path) }
          it { should_not have_link('Sign in', href: signin_path) }
    end
end
class SessionsController < ApplicationController

  def new
  end

  def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])

    else
      flash.now[:error] = 'Invalid email/password combination'
      render 'new'
    end
  end

  def destroy
  end
end
<% provide(:title, "Sign in") %>
<h1>Sign in</h1>

<div class="row">
  <div class="span6 offset3">
    <%= form_for(:session, url: sessions_path) do |f| %>

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

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

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

    <p>New user? <%= link_to "Sign up now!", signup_path %></p>
  </div>
</div>
SampleApp::Application.routes.draw do
  resources :users 
  resources :sessions,      only: [:new, :create, :destroy]
  root to: 'static_pages#home'
  match '/signup',  to: 'users#new',            via: 'get'
  match '/signin',  to: 'sessions#new',         via: 'get'
  match '/signout', to: 'sessions#destroy',     via: 'delete'
  match '/help',    to: 'static_pages#help',    via: 'get'
  match '/about',   to: 'static_pages#about',   via: 'get'
  match '/contact', to: 'static_pages#contact', via: 'get'

  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
  # root to: 'welcome#index'

  # Example of regular route:
  #   get 'products/:id' => 'catalog#view'

  # Example of named route that can be invoked with purchase_url(id: product.id)
  #   get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

  # Example resource route (maps HTTP verbs to controller actions automatically):
  #   resources :products

  # Example resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end

  # Example resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end

  # Example resource route with more complex sub-resources:
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get 'recent', on: :collection
  #     end
  #   end

  # Example resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end
end
def create
  @user = User.new(user_params)

  if @user.save
    redirect_to @user    #This statement is important
  else
    render 'new'
  end
end