将user_path路由到用户/索引而不是用户/ id

时间:2013-09-13 14:03:27

标签: ruby-on-rails ruby

我正在尝试通过railstutorial.org上的Rails教程来解决这个问题。我在测试中的user_path应该转到单个用户的个人资料页面,而是转到用户/索引(我还没有创建它)。

/ *我的测试代码* /

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

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

  end

/ *失败:* /

 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

routes文件显示user_path应该给我user / {id}左右

> 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

3 个答案:

答案 0 :(得分:3)

在调用访问user_path(@user)

之前,您应首先使用 @ user.save!保存用户

因为在您保存用户之前,它不会持久存储在数据库中,也不会有任何ID。

describe "profile page" do
  #let(user) { FactoryGirl.create(:user) }
  @user = User.new(name: "Example User", email: "user@example.com",
                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

答案 1 :(得分:1)

您是否检查过@user是否有值?

user_path(@user)将转到/users/<@user.id>,但如果@user为nil,则会尝试/users/。鉴于您还没有创建索引方法和视图,这可能就是它失败的原因。

感谢您的测试代码。首先,您的用户不在数据库中 - 将.new更改为.create或添加@user.save。其次,您的params[:id]未在测试代码中填充,因此查找失败(将controller.params[:id] = @user.id添加到您的测试中)。 (第三,在上一次测试中你有一个拼写错误shoult而不是should。)

答案 2 :(得分:0)

您似乎没有包含测试代码,这对解决此问题非常有帮助。最有可能的是,你在测试中将@user设置为nil,修复它将解决问题。