2011-11-09 40 views
3

我有以下的事情:沒有路由匹配{:動作=> 「秀」,:控制器=> 「用戶」}錯誤

it "should redirect to the user show page" do 
    post :create, :user => @attr 
    response.should redirect_to(user_path(assigns(:user))) 
end 

在我users_controller:

在users_controller_spec RSpec的測試我有以下幾點:

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

def create 
    @title = "Sign up" 
    @user = User.new(params[:user]) 
    if @user.save 
    redirect_to @user, :notice => "Signed Up!" 
    else 
    @title = "Sign up" 
    render "new" 
    end 
end 

在我的routes.rb我有以下幾點:

Psra::Application.routes.draw do 
    resources :users 
    resources :sessions 

    # Root Route 

    root :to => 'pages#home' 

    # Pages Routes 

    match '/contact', :to => 'pages#contact' 
    match '/about', :to => 'pages#about' 
    match '/help', :to => 'pages#help' 
    match '/signup', :to => 'users#new' 

    # Users Route 

    match '/signup', :to => 'users#new' 

    #Sessions Routes 
    get "logout" => "sessions#destroy", :as => "logout" 
    get "login" => "sessions#new", :as => "login" 

end 

這裏是我的耙路線

 users GET /users(.:format)    {:action=>"index", :controller=>"users"} 
      POST /users(.:format)    {:action=>"create", :controller=>"users"} 
    new_user GET /users/new(.:format)   {:action=>"new", :controller=>"users"} 
    edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"} 
     user GET /users/:id(.:format)   {:action=>"show", :controller=>"users"} 
      PUT /users/:id(.:format)   {:action=>"update", :controller=>"users"} 
      DELETE /users/:id(.:format)   {:action=>"destroy", :controller=>"users"} 
    sessions GET /sessions(.:format)   {:action=>"index", :controller=>"sessions"} 
      POST /sessions(.:format)   {:action=>"create", :controller=>"sessions"} 
new_session GET /sessions/new(.:format)  {:action=>"new", :controller=>"sessions"} 
edit_session GET /sessions/:id/edit(.:format) {:action=>"edit", :controller=>"sessions"} 
    session GET /sessions/:id(.:format)  {:action=>"show", :controller=>"sessions"} 
      PUT /sessions/:id(.:format)  {:action=>"update", :controller=>"sessions"} 
      DELETE /sessions/:id(.:format)  {:action=>"destroy", :controller=>"sessions"} 
     root  /       {:controller=>"pages", :action=>"home"} 
    contact  /contact(.:format)   {:controller=>"pages", :action=>"contact"} 
     about  /about(.:format)    {:controller=>"pages", :action=>"about"} 
     help  /help(.:format)    {:controller=>"pages", :action=>"help"} 
     signup  /signup(.:format)   {:controller=>"users", :action=>"new"} 
        /signup(.:format)   {:controller=>"users", :action=>"new"} 
     logout GET /logout(.:format)   {:action=>"destroy", :controller=>"sessions"} 
     login GET /login(.:format)    {:action=>"new", :controller=>"sessions"} 

這一切都導致了以下錯誤:

1) UsersController POST 'create' success should redirect to the user show page 
    Failure/Error: response.should redirect_to(user_path(assigns(:user))) 
    ActionController::RoutingError: 
     No route matches {:action=>"show", :controller=>"users"} 
    # ./spec/controllers/users_controller_spec.rb:95:in `block (4 levels) in <top (required)>' 

任何想法什麼我做錯了嗎?

+1

這是從瀏覽器工作嗎?不是來自測試 – vladdruzh

+0

是的,它可以在瀏覽器中運行。 –

回答

4

它看起來像show行動沒有得到它需要獲得正確的頁面的用戶信息。 assigns方法只是創建一個實例變量。調用user_path將需要用戶模擬或對象來使呼叫正常工作。

+0

我的理解是,分配需要:用戶並將其轉換爲對象。這是不正確的? –

+0

'assigns'實際上不會返回一個對象。我相信這是問題的根源。 –

相關問題