2
votes

I feel like I have everything I need in my routes.rb, my controller, and my controller spec, but for some reason I'm still getting a routing error (ActionController::RoutingError). Here's my controller:

class HunchController < ActionController::Base
  protect_from_forgery

  def results
    auth_token_key = params[:auth_token_key]
    user_id = params[:user_id]
    @user = User.create!
    @user.auth_token = @user.get_auth_token(auth_token_key, user_id)
    @recommended_books = @user.get_recommended_books(@user.auth_token)
  end
end

Here's my controller spec:

require 'spec_helper'

describe HunchController do
  describe "POST 'results'" do
    before do
      @params = {
        :auth_token_key => "my auth token",
        :user_id => "my user id"
      }
    end

    it "succeeds" do
      post :results, @params
      response.should be_success
    end
  end
end

And here's my routes.rb:

MyApplicationName::Application.routes.draw do
  root :to => 'hunch#index'

  resources :users
  post 'hunch/results' => "hunch#results"
  match '/results' => 'hunch#results'
end

EDIT: Here's my rake routes output:

         root        /                         {:controller=>"pages", :action=>"index"}
hunch_results POST   /hunch/results(.:format)  {:controller=>"hunch", :action=>"results"}
      results        /results(.:format)        {:controller=>"hunch", :action=>"results"}
        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"}

EDIT #2: I'm getting this error with my users#show test too. Here's the actual error:

  1) UsersController#show succeeds
     Failure/Error: get :show
     ActionController::RoutingError:
       No route matches {:controller=>"users", :action=>"show"}
     # ./spec/controllers/users_controller_spec.rb:13:in `block (3 levels) in <top (required)>'
1
Please add your rake routes output.John
What's the actual routing error?Andrew Marshall
Given that routing error you've posted the wrong spec. The error is in the UsersController spec, not the HunchController spec.Andrew Marshall

1 Answers

0
votes

Ok, I got the same problem when gem journey was updated to the latest version. And find that if you got a route like this:

user GET    /users/:id(.:format)      {:action=>"show", :controller=>"users"}

which have a param (id in this case), you necessarily need to render a link in template or use a user_path/user_url variables in controller with param too, so in your templates try to find:

<%= link_to "linkname", user_path %>

and replace by

<%= link_to "linkname", user_path(@user) %>

or search in any controller for the user_path/user_url usage and replace them by user_path(@user)/user_url(@user) accordingly.