Including Rails path helper causes exception in initializer

1,340 views
Skip to first unread message

Jan Hecking

unread,
Mar 6, 2013, 9:35:10 PM3/6/13
to ruby-...@googlegroups.com
I've mounted my Grape API(s) under Rails and would like to use Rails' path helpers. I followed the how-to at https://github.com/intridea/grape/wiki/Grape-and-Rails-Path-Helpers and copied the code into an initializer verbatim. However when I start Rails I get the following exception:

railties-3.2.12/lib/rails/railtie/configuration.rb:85:in `method_missing': undefined method `routes' for #<Rails::Application::Configuration:0x007fac57fb80f0> (NoMethodError)
from config/initializers/grape.rb:22:in `<class:Endpoint>'
from config/initializers/grape.rb:20:in `<module:Grape>'
from config/initializers/grape.rb:19:in `<top (required)>'
from activesupport-3.2.12/lib/active_support/dependencies.rb:245:in `load'
        ...

Including Rails.application.routes.url_helpers directly in my Grape::API subclass does work. But when I want to use a method such as root_url it still throws an unknown method error:

NameError - undefined local variable or method `root_url' for API::Partner:Class:
  app/api/partner.rb:9:in `<class:Partner>'
  app/api/partner.rb:2:in `<module:API>'
  app/api/partner.rb:1:in `<top (required)>'

Any tips for how to get the url_helpers working in Grape? We're running Rails 3.2.12 in case that's relevant. 

Jan

Daniel Doubrovkine

unread,
Mar 8, 2013, 1:18:46 PM3/8/13
to ruby-...@googlegroups.com
This is a total guess, but I think it's a bug and was introduced in https://github.com/intridea/grape/pull/269.

Can you please write this up as an issue? A test reproducing the problem would be awesome, too.


Jan

--
You received this message because you are subscribed to the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--

dB. | Moscow - Geneva - Seattle - New York
dblock.org - @dblockdotorg

Marcus Geißler

unread,
Mar 18, 2013, 9:45:18 AM3/18/13
to ruby-...@googlegroups.com
I'm facing the same error for the grape 0.4 used in a rails 3.2.12 app.

a...@substantial.com

unread,
Mar 21, 2013, 1:32:26 PM3/21/13
to ruby-...@googlegroups.com
I was able to fix this with the following code:

module Grape
  class Endpoint
    include Rails.application.routes.url_helpers

    default_url_options[:host] = ::Rails.application.routes.default_url_options[:host] || "www.example.com"
  end
end

I think the key is that Rails.application.config.routes should be Rails.application.routes

Jan Hecking

unread,
Mar 22, 2013, 3:10:18 AM3/22/13
to ruby-...@googlegroups.com
Thanks! So it was just a simple typo... Good job catching this - I started at this code for dont-know-how-long and didn't notice the default_url_options call was plain wrong.

I've updated the Grape wiki to fix this error as well.

Cheers,
Jan


--
You received this message because you are subscribed to a topic in the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ruby-grape/riqLwEeAhyw/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to ruby-grape+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages