How to use url helpers in lib modules, and set host for multiple environments

前端 未结 4 626
情深已故
情深已故 2020-12-25 14:43

In a Rails 3.2 app I need to access url_helpers in a lib file. I\'m using

Rails.application.routes.url_helpers.model_url(model)
<
相关标签:
4条回答
  • 2020-12-25 15:20

    This is a problem that I keep running into and has bugged me for a while.

    I know many will say it goes against the MVC architecture to access url_helpers in models and modules, but there are times—such as when interfacing with an external API—where it does make sense.

    And now thanks to this great blog post I've found an answer!

    #lib/routing.rb
    
    module Routing
      extend ActiveSupport::Concern
      include Rails.application.routes.url_helpers
    
      included do
        def default_url_options
          ActionMailer::Base.default_url_options
        end
      end
    end
    
    #lib/url_generator.rb
    
    class UrlGenerator
      include Routing
    end
    

    I can now call the following in any model, module, class, console, etc

    UrlGenerator.new.models_url
    

    Result!

    0 讨论(0)
  • 2020-12-25 15:23

    A slight improvement (at least for me) on Andy's lovely answer

    module UrlHelpers
    
      extend ActiveSupport::Concern
    
      class Base
        include Rails.application.routes.url_helpers
    
        def default_url_options
          ActionMailer::Base.default_url_options
        end
      end
    
      def url_helpers
        @url_helpers ||= UrlHelpers::Base.new
      end
    
      def self.method_missing method, *args, &block
        @url_helpers ||= UrlHelpers::Base.new
    
        if @url_helpers.respond_to?(method)
          @url_helpers.send(method, *args, &block)
        else
          super method, *args, &block
        end
      end
    
    end
    

    and the way you use it is:

    include UrlHelpers
    url_helpers.posts_url # returns https://blabla.com/posts
    

    or simply

    UrlHelpers.posts_url # returns https://blabla.com/posts
    

    Thank you Andy! +1

    0 讨论(0)
  • 2020-12-25 15:29

    Use this string in any module controller to get application URL-helpers works in any view or controller.

    include Rails.application.routes.url_helpers
    

    Please note, some internal module url-helpers should be namespaced.

    Example: root application

    routes.rb

    Rails.application.routes.draw do
    get 'action' =>  "contr#action", :as => 'welcome'
    mount Eb::Core::Engine => "/" , :as => 'eb'
    end
    

    Url helpers in module Eb:

    users_path
    

    Add include Rails.application.routes.url_helpers in controller contr

    So after that helper should be

    eb.users_path
    

    So inside Eb module you can use welcome_path same as in root application!

    0 讨论(0)
  • 2020-12-25 15:31

    Not sure if this works in Rails 3.2, but in later versions setting default url options for the routes can be done directly on the routes instance.

    So for example, to set the same options as for ActionMailer:

    Rails.application.routes.default_url_options = ActionMailer::Base.default_url_options
    
    0 讨论(0)
提交回复
热议问题