Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No Route Matches ... Rails Engine

So I keep getting the error:

No route matches {:action=>"create", :controller=>"xaaron/api_keys"}

Which is thrown in the test:

it "should not create an api key for those not logged in" do
  post :create
  expect(response).to redirect_to xaaron.login_path
end

when I go to spec/dummy and run the rake routes command I see:

       api_keys GET    /api_keys(.:format)                 xaaron/api_keys#index
                POST   /api_keys(.:format)                 xaaron/api_keys#create
    new_api_key GET    /api_keys/new(.:format)             xaaron/api_keys#new
   edit_api_key GET    /api_keys/:id/edit(.:format)        xaaron/api_keys#edit
        api_key GET    /api_keys/:id(.:format)             xaaron/api_keys#show
                PATCH  /api_keys/:id(.:format)             xaaron/api_keys#update
                PUT    /api_keys/:id(.:format)             xaaron/api_keys#update
                DELETE /api_keys/:id(.:format)             xaaron/api_keys#destroy

Which shows that yes this route does exist. My routes file for this engine looks like:

Xaaron::Engine.routes.draw do
      get 'login' => 'sessions#new', :as => 'login'
      get 'logout' => 'sessions#destroy', :as => 'logout'
      get 'signup' => 'users#new', :as => 'signup'
      get 'permission_denied' => 'error#denied', :as => 'permission_denied'
      get 'record_not_found' => 'error#error', :as => 'record_not_found'
      get 'password_reset' => 'password_resets#edit', :as => 'rest_user_password'

      resource :error, controller: 'error'

      resources :users
      resources :api_keys
      resources :sessions
      resources :roles
      resources :password_resets
end

What am I missing?

update

For those of you curious how I am getting these routes, its because the dummy app's routes file is set up (by default) as such:

Rails.application.routes.draw do

  mount Xaaron::Engine => "/xaaron"
end

Update II

I have been reading this api docs on how routing is done in engines and I believe the way I have done this is correct, how ever the controller is defined as such:

module Xaaron
  class ApiKeysController < ActionController::Base
    before_action :authenticate_user!

    def index
      @api_key = Xaaron::ApiKey.where(:user_id => current_user.id)
    end

    def create
      @api_key = Xaaron::ApiKey.new(:user_id => current_user.id, :api_key => SecureRandom.hex(16))
      create_api_key(@api_key)
    end

    def destroy
      Xaaron::ApiKey.find(params[:id]).destroy
      flash[:notice] = 'Api Key has been deleted.'
      redirect_to xarron.api_keys_path
    end
  end
end
like image 876
user3379926 Avatar asked Apr 07 '14 23:04

user3379926


1 Answers

You need to tell your spec you are using the engine routes:

describe ApiKeysController do
  routes { Xaaron::Engine.routes }

  it "should not create an api key for those not logged in" do
    post :create
    expect(response).to redirect_to xaaron.login_path
  end
end
like image 155
Logan Serman Avatar answered Oct 04 '22 02:10

Logan Serman