Rajesh Chamarthi
Rajesh Chamarthi

Reputation: 18808

Rails user_path routing to users/index instead of users/id

I am trying to work through the Rails tutorial at railstutorial.org and am stuck at this problem. My user_path in the test is supposed to go to the individual user's profile page, but is instead going to users/index (which I haven't created yet, on purpose).

/* My test code */

  describe "profile page" do
    #let(user) { FactoryGirl.create(:user) }
    @user = User.new(name: "Example User", email: "[email protected]",
                    password: "foobar", password_confirmation: "foobar")
    before { visit user_path(@user) }

    it { should have_content(user.name) }
    it { shoult have_title(user.name)   }

  end

/* Failures: */

 1) UserPages profile page
    ←[31mFailure/Error:←[0m ←[31mbefore { visit user_path(@user) }←[0m
    ←[31mActionView::MissingTemplate←[0m:
      ←[31mMissing template users/index, application/index with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffe
}. Searched in:←[0m
      ←[31m  * "C:/Users/rajeshch/Documents/Projects/sample_app/app/views"←[0m
[36m     # C:in `find_template'←[0m
[36m     # ./spec/requests/user_pages_spec.rb:19:in `block (3 levels) in <top (required)>'←[0m

The routes file shows that user_path should give me users/{id} or so

> rake routes
   Prefix Verb   URI Pattern               Controller#Action
    users GET    /users(.:format)          users#index
          POST   /users(.:format)          users#create
 new_user GET    /users/new(.:format)      users#new
edit_user GET    /users/:id/edit(.:format) users#edit
     user GET    /users/:id(.:format)      users#show      <<<------
          PATCH  /users/:id(.:format)      users#update
          PUT    /users/:id(.:format)      users#update
          DELETE /users/:id(.:format)      users#destroy
     root GET    /                         static_pages#home
   signup GET    /signup(.:format)         users#new
     help GET    /help(.:format)           static_pages#help
    about GET    /about(.:format)          static_pages#about
  contact GET    /contact(.:format)        static_pages#contact

/* users_controller.rb*/

class UsersController < ApplicationController

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

    def new
    end

end

/* config/routes.rb */

SampleApp::Application.routes.draw do

  resources :users
  root 'static_pages#home'

  match '/signup',  to: 'users#new',            via: 'get'
  match '/help',    to: 'static_pages#help',    via: 'get'
  match '/about',   to: 'static_pages#about',   via: 'get'
  match '/contact', to: 'static_pages#contact', via: 'get'

end

Upvotes: 0

Views: 2316

Answers (3)

Aditya Kamatar
Aditya Kamatar

Reputation: 363

You should save the User first with @user.save! before calling visit user_path(@user)

Because until you save the user it wont persist in the database and will not have any id.

describe "profile page" do
  #let(user) { FactoryGirl.create(:user) }
  @user = User.new(name: "Example User", email: "[email protected]",
                password: "foobar", password_confirmation: "foobar")

 # Add this to your code ..
  @user.save! 

  before { visit user_path(@user) }

  it { should have_content(user.name) }
  it { shoult have_title(user.name)   }

end

Upvotes: 3

user740584
user740584

Reputation:

Have you checked if @user has a value?

user_path(@user) will go to /users/<@user.id> but if @user is nil, it will try to go /users/. Given you haven't created the index method and views yet, that will probably be why it fails.

Thanks for the test code. Firstly your user is not in the database - change the .new to .create or add a @user.save. Secondly your params[:id] is not populated in the test code so the find will fail (add controller.params[:id] = @user.id to your test). (Thirdly you have a typo shoult instead of should in the last test.)

Upvotes: 1

Ben K.
Ben K.

Reputation: 21

It doesn't seem like you included your test code, which would be immensely helpful in figuring out this problem. Most likely, you have @user set as nil in the test, fixing that will solve the problem.

Upvotes: 0

Related Questions