Starkers
Starkers

Reputation: 10531

No route matches error when route exists

Here's the code I'm testing:

before_action :set_location, only: [:show]
def create
    @location = Location.new( location_parameters )

    if @location.save
        redirect_to location_path(@location.id)
    else
        render action: :new, status: 422
    end
end

And here are my routes:

resources :locations

And here's the expectation:

before { request }
subject { response }

it{ should redirect_to controller: 'locations', action: 'show'}

And yet I'm getting this error that 'locations#show' doesn't exist, even though raking the routes shows that they do exist:

Failures:

  1) LocationsController#create response with valid request should redirect to [:controller, "locations"] and [:action, "show"]
     Failure/Error: it{ should redirect_to controller: 'locations', action: 'show'}
     ActionController::UrlGenerationError:
       No route matches {:action=>"show", :controller=>"locations"}
     # ./spec/controllers/locations_controller_spec.rb:16:in `block (5 levels) in <top (required)>'

Finished in 1.5 seconds (files took 3.34 seconds to load)
17 examples, 1 failure, 5 pending

Failed examples:

rspec ./spec/controllers/locations_controller_spec.rb:16 # LocationsController#create response with valid request should redirect to [:controller, "locations"] and [:action, "show"]
starkers@ubuntu:~/Documents/currentTraining/rspec/geo$ rake routes
       Prefix Verb   URI Pattern                   Controller#Action
    locations GET    /locations(.:format)          locations#index
              POST   /locations(.:format)          locations#create
 new_location GET    /locations/new(.:format)      locations#new
edit_location GET    /locations/:id/edit(.:format) locations#edit
     location GET    /locations/:id(.:format)      locations#show
              PATCH  /locations/:id(.:format)      locations#update
              PUT    /locations/:id(.:format)      locations#update
              DELETE /locations/:id(.:format)      locations#destroy

Any reasons for this?

Upvotes: 0

Views: 726

Answers (1)

arthurnum
arthurnum

Reputation: 68

'show' action takes 'id' param

location GET    /locations/:id(.:format)      locations#show

Try something like

it{ should redirect_to controller: 'locations', action: 'show', id: <yourLocationObj>.id }

Or

it{ should redirect_to(assigns(:location)) }

Upvotes: 1

Related Questions