Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Weird problem with routes

routes rails rspec

  • Please log in to reply
1 reply to this topic

#1 Ohm

Ohm

    Guard

  • Members
  • 184 posts
  • LocationCopenhagen

Posted 11 October 2013 - 10:07 AM

I have this error controller:

class ErrorsController < ApplicationController
  def error_404
    @not_found_path = params[:not_found]
    @return_page = request.referer || root_path
    errors_respond 404
  end

  def error_500
    errors_respond 500
  end
end

and in my applicaiton_controller.rb I have

def render_error(status, exception)
  errors_respond status
rescue
end

The render_error method is defined in the application_helper.rb-file

def errors_respond(status)
  respond_to do |format|
    format.html { render :template => "errors/error_#{status}", :layout => 'layouts/application_bootstrap', :status => status }
    format.all { render :nothing => true, :status => status }
  end
end

At the very bottom of my routes.rb-file I have

match '/i_really_do_not_exist', to: redirect('/') # WTF? error_controller_specs will fail if this is removed
match '*not_found', to: 'errors#error_404'

The mentioned error_controller_spec.rb is this

require 'spec_helper'

describe ErrorsController do
  describe "GET 'error_404'" do
    it "returns http status 404" do
      get 'error_404'
      response.response_code.should == 404
    end
  end

  describe "GET 'error_500'" do
    it "returns http status 500" do
      get 'error_500'
      response.response_code.should == 500
    end
  end
end

If I run them without the '/i_really_do_not_exist' path, they fail with

ActionController::RoutingError:
No route matches {:controller=>"errors", :action=>"error_404"}

The same is true if I removed the /

 

I can change both the matching part and the redirecting part and my specs will pass, however they fail if I remove it entirely.

 

Anybody knows what's up?

 

Edit: I also posted it at StackOverflow.


Blog: http://ohm.sh | Twitter: madsohm


#2 james

james

    Guard

  • Moderators
  • 221 posts
  • LocationLeeds, U.K.

Posted 11 October 2013 - 09:07 PM

Catch all routes are not a solution to your problem, engines that define their own routes won't be caught. Gems like Devize spring to mind.

Actually, there is no simple solution to this other than to write your own engine to handle the errors.

 

I did come across this once which may well help

 

https://github.com/v...s-routing_error


Programming is just about problem solving!






Also tagged with one or more of these keywords: routes, rails, rspec

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users