2017-10-29 27 views
0

にリダイレクトするために見つけることができませんでした。次のようにテストがある:私は奇妙なエラーが発生し、このテストに合格するとコントローラは、idが、私はこのコントローラを持って

require 'rails_helper' 

RSpec.describe SalesmenController, type: :controller do 
    include Devise::Test::ControllerHelpers 

    before(:each) do 
    @request.env["devise.mapping"] = Devise.mappings[:owner] 
    @current_owner = FactoryGirl.create(:owner) 
    sign_in @current_owner 
    @current_company = FactoryGirl.create(:company, owner: @current_owner) 
    end 

describe "POST #create" do 
    before(:each) do 
     salesman = FactoryGirl.create(:salesman, company: @current_company) 
     post :create, params: {:company_id => @current_company.id, salesman: { name: salesman.name, company_id: @current_company.id } } 
    end 

    it "redirect to new team" do 
     expect(response).to have_http_status(:success) 
    end 

    it "Create team with right attributes" do 
     expect(Salesman.last.company).to eql(@current_company) 
     expect(Salesman.last.name).to eql(@salesman[:name]) 
    end 
    end 
end 

は、コントローラで、私がショーのルートにリダイレクトするように指示する際のIDを取得することができないようです。エラーがある:それはセールスマンのIDを欠けている

1) SalesmenController POST #create redirect to new team 
    Failure/Error: redirect_to company_salesman_path(@salesman.id) 

    ActionController::UrlGenerationError: 
     No route matches {:action=>"show", :company_id=>390, :controller=>"salesmen"} missing required keys: [:id] 

、私は理由を理解していないと私はこのような状況から抜け出す方法がわかりません。

私のルートは以下のとおりです。

Rails.application.routes.draw do 
    resources :companies do 
    resources :salesmen 
    resources :goals do 
     resources :days 
    end 
    end 
    devise_for :owners, :controllers => { registrations: 'registrations' } 
end 

答えて

0

redirect_to company_salesman_path(:id => @salesman.id) 

または

redirect_to company_salesman_path(@salesman) 

を試してみて、これは役立つだろう願っています。

+0

私はテストに合格しましたが、失敗/エラー:expect(応答)have_http_status(:success) 応答が成功ステータスコード(2xx) 302 –

+0

でした。応答のステータスを:foundの代わりにfoundとしました。 –

+0

私はこの[link](http://billpatrianakos.me/blog/2013/10/13/list-of-rails-status-code-symbols/)が助けになると思います。この[link](https:///stackoverflow.com/questions/32883254/http-status-code-302)too too –

関連する問題