ruby on rails - ActionController::UrlGenerationError - with route defined and action in controller, still getting error for no route -
i'm getting following error in rspec when running schools_controller_spec.rb test:
actioncontroller::urlgenerationerror: no route matches {:action=>"show", :controller=>"schools"}
what's puzzling me have routes configured, , action defined in appropriate controller. i'm not getting error other tests in spec, such 'get #index'
, etc. running rails 4.2 rspec/capybara.
here's routes.rb:
rails.application.routes.draw root to: 'pages#home', id: 'home' resources :users resources :schools resource :session, only: [:new, :create, :destroy] match '/home', to: 'pages#home', via: 'get', as: 'home_page' end
rake routes returns:
schools /schools(.:format) schools#index post /schools(.:format) schools#create new_school /schools/new(.:format) schools#new edit_school /schools/:id/edit(.:format) schools#edit school /schools/:id(.:format) schools#show patch /schools/:id(.:format) schools#update put /schools/:id(.:format) schools#update delete /schools/:id(.:format) schools#destroy
there's route defined on fifth line, schools#show.
the schools_controller.rb:
class schoolscontroller < applicationcontroller before_action :require_signin before_filter :admin_only, except: :index, :show def index @schools = school.all end def show # code pending end private def admin_only unless current_user.admin? redirect_to :back, alert: "access denied." end end end
the link individual school seems defined in view helper (_school.html.haml):
%li#schools = link_to school.name, school = school.short_name = school.city = school.state
and looking @ front-end html confirms it's working correctly. can see, example: <a href="/schools/1">community college of air force</a>
. when click link page shows following in debug dump:
--- !ruby/hash:actioncontroller::parameters controller: schools action: show id: '1'
finally, measure, here's spec file (schools_controller_spec.rb):
require 'rails_helper' describe schoolscontroller, type: :controller # specs omitted other actions describe 'get #show' context "when not signed in" "returns 302 redirect code" :show expect(response.status).to eq 302 end "redirects signin page" :show expect(response).to redirect_to new_session_path end end context "when signed in user" before :each @user = double(:user) allow(controller).to receive(:current_user).and_return @user @school = create(:school) end "assigns school @school variable" :show expect(assigns(:school)).to eq @school end end end end
the route appears in rake routes. method defined in appropriate controller. there don't appear silly naming errors (e.g. plural/singular). spec doesn't appear have issues routing #index or other routes example. works exactly expected in browser.
so why keep getting "no route matches" error when run controller spec?
this because show action expecting id aren't passing. replace:
get :show
with this:
get :show, id: school.id
the above assumes have school variable, perhaps let in before block?
let(:school) { create(:school) }
Comments
Post a Comment