Rails url_for and namespaced models

前端 未结 4 1422
迷失自我
迷失自我 2020-12-29 08:13

In Rails, is it possible to namespace models in modules and still get correct behavior from url_for?

For instance, here, url_for works as e

相关标签:
4条回答
  • 2020-12-29 08:24

    Use

    namespace "blah" do
      resources :thing
    end
    

    Then routes will be named appropiately.

    rake routes
    

    To view all routes

    0 讨论(0)
  • 2020-12-29 08:31

    Specify the module on the route

    resources :users, :module => "m"
    

    or use scope to do it

    scope :module => "m" do
      resources :users
    end
    
    0 讨论(0)
  • 2020-12-29 08:38

    Just define use_relative_model_naming? in the containing module to avoid prefixing the generated route names:

    module M
      def self.use_relative_model_naming?
        true
      end
    end
    
    0 讨论(0)
  • 2020-12-29 08:39

    In my case I overridden the url_for method on my application_helper.rb file to add the :network param on all routes from my namespace :mkp.

    module ApplicationHelper
      def url_for(options = {})
        if options.is_a?(Hash) && options.has_key?(:controller)
          if options[:network].nil? && options[:controller].match(/^mkp\//).present?
            options[:network] = @network.downcase
          end
        end
        super(options)
      end
    end
    
    0 讨论(0)
提交回复
热议问题