Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby on Rails Tutorial (mhartl) "Chapter 10: Destroying User" autotest rspec DELETE 'destroy' failure
    primarykey
    data
    text
    <p>I am doing Michael Hartl's Rails Tutorial screencast + online book on Chapter 10, and I am stuck at the very last sub-chapter Destroying User.</p> <p><strong>Console output:</strong></p> <pre><code>Failures: 1) UsersController DELETE 'destroy' as an admin user should destroy the user Failure/Error: delete :destroy, :id =&gt; @user NameError: undefined local variable or method `users' for #&lt;UsersController:0x00000104a2f698&gt; ./app/controllers/users_controller.rb:47:in `destroy' ./spec/controllers/users_controller_spec.rb:321:in `block (5 levels) in &lt;top (required)&gt;' ./spec/controllers/users_controller_spec.rb:320:in `block (4 levels) in &lt;top (required)&gt;' 2) UsersController DELETE 'destroy' as an admin user should redirect to the users page Failure/Error: delete :destroy, :id =&gt; @user NameError: undefined local variable or method `users' for #&lt;UsersController:0x000001049bf370&gt; ./app/controllers/users_controller.rb:47:in `destroy' ./spec/controllers/users_controller_spec.rb:326:in `block (4 levels) in &lt;top (required)&gt;' </code></pre> <p><strong>users_controller.rb</strong></p> <pre><code>class UsersController &lt; ApplicationController before_filter :authenticate, :only =&gt; [:index, :edit, :update, :destroy] before_filter :correct_user, :only =&gt; [:edit, :update] before_filter :admin_user, :only =&gt; :destroy def index @users = User.paginate(:page =&gt; params[:page]) @title = "All users" end def show @user = User.find(params[:id]) @title = @user.name end def new @user = User.new @title = "Sign up" end def create @user = User.new(params[:user]) if @user.save sign_in @user redirect_to @user, :flash =&gt; { :success =&gt; "Welcome to the Sample App!" } else @title = "Sign up" render 'new' end end def edit @title = "Edit user" end def update if @user.update_attributes(params[:user]) redirect_to @user, :flash =&gt; { :success =&gt; "Profile updated." } else @title = "Edit user" render 'edit' end end def destroy User.find(params[:id]).destroy redirect_to users.path, :flash =&gt; { :success =&gt; "User destroyed." } end private def authenticate deny_access unless signed_in? end def correct_user @user = User.find(params[:id]) redirect_to(root_path) unless current_user?(@user) end def admin_user @user = User.find(params[:id]) redirect_to(root_path) if !current_user.admin? || current_user?(@user) end end </code></pre> <p><strong>Users_controller_spec.rb:</strong></p> <pre><code> describe "DELETE 'destroy'" do before(:each) do @user = Factory(:user) end describe "as a non-signed-in user" do it "should deny access" do delete :destroy, :id =&gt; @user response.should redirect_to(signin_path) end end describe "as non-admin user" do it "should protect the action" do test_sign_in(@user) delete :destroy, :id =&gt; @user response.should redirect_to(root_path) end end describe "as an admin user" do before(:each) do @admin = Factory(:user, :email =&gt; "admin@example.com", :admin =&gt; true) test_sign_in(@admin) end it "should destroy the user" do lambda do delete :destroy, :id =&gt; @user end.should change(User, :count).by(-1) end it "should redirect to the users page" do delete :destroy, :id =&gt; @user flash[:success].should =~ /destroyed/i response.should redirect_to(users_path) end it "should not be able to destroy itself" do delete :destroy, :id =&gt; @admin lambda do delete :destroy, :id =&gt; @admin end.should_not change(User, :count) end end end end </code></pre> <p>I am a beginner but always in a habit of doing extensive researches (90% of the time I found answers by searching SO and Google) before posting any question here. </p> <p>My last 2 unposted questions (found the answer before posting) I had was fixed by updating my Gemfiles (will_paginate and undefined _selector in rspec). Could this be it too? </p> <p>Thanks for the concern and taking the time to read this. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload