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

前端 未结 4 630
情深已故
情深已故 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: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!

提交回复
热议问题