Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 具有主机约束的Rails 4 url__Ruby On Rails_Ruby On Rails 3_Ruby On Rails 4_Routing - Fatal编程技术网

Ruby on rails 具有主机约束的Rails 4 url_

Ruby on rails 具有主机约束的Rails 4 url_,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-4,routing,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 4,Routing,我们创建了一个包含多个站点的多租户应用程序,每个站点都可以从后端修改路由 在routes.rb中,我们加载所有站点的动态路由,并将它们放入主机约束中,如下所示 routes.rb Frontend::Application.routes.draw do DynamicRoutes.load end class DynamicRoutes # dynamically loads the routes from settings into the routes.rb file #

我们创建了一个包含多个站点的多租户应用程序,每个站点都可以从后端修改路由

在routes.rb中,我们加载所有站点的动态路由,并将它们放入主机约束中,如下所示

routes.rb

Frontend::Application.routes.draw do

  DynamicRoutes.load

end
class DynamicRoutes
  # dynamically loads the routes from settings into the routes.rb file
  # and adds a host constraint to just match with the current sites host
  # http://codeconnoisseur.org/ramblings/creating-dynamic-routes-at-runtime-in-rails-4
  def self.load
    if Site.table_exists?
      Frontend::Application.routes.draw do
        Site.includes(:setting).each do |site|
          site.routes.each do |route|
            # write the route with the host constraint
            constraints(:host => site.hostname) do
              case route[0]
              when :shop_show
                match "#{route[1]}", to: 'shops#show', via: [:get], as: "shop_show_#{site.id}"
              end
            end
          end
        end
      end
    end
  end

  # allows to reload the routing
  # e.g. when changes in route settings where made
  #
  def self.reload
    Rails.application.reload_routes!
  end
end
%w( shop_show ).each do |helper|
  helper_name = "#{helper}_path".to_sym
  helper_method helper_name
  define_method(helper_name) { |*args| send "#{helper}_#{site.id}_path", *args }
end
app/models/dynamic_routes.rb

Frontend::Application.routes.draw do

  DynamicRoutes.load

end
class DynamicRoutes
  # dynamically loads the routes from settings into the routes.rb file
  # and adds a host constraint to just match with the current sites host
  # http://codeconnoisseur.org/ramblings/creating-dynamic-routes-at-runtime-in-rails-4
  def self.load
    if Site.table_exists?
      Frontend::Application.routes.draw do
        Site.includes(:setting).each do |site|
          site.routes.each do |route|
            # write the route with the host constraint
            constraints(:host => site.hostname) do
              case route[0]
              when :shop_show
                match "#{route[1]}", to: 'shops#show', via: [:get], as: "shop_show_#{site.id}"
              end
            end
          end
        end
      end
    end
  end

  # allows to reload the routing
  # e.g. when changes in route settings where made
  #
  def self.reload
    Rails.application.reload_routes!
  end
end
%w( shop_show ).each do |helper|
  helper_name = "#{helper}_path".to_sym
  helper_method helper_name
  define_method(helper_name) { |*args| send "#{helper}_#{site.id}_path", *args }
end
因此,我们为每个站点创建所有路由,并将它们与主机约束匹配。除非我们为帮助程序使用url\u,否则这可以正常工作

@site = Site.find_by(hostname: request.host)
url_for controller: 'shop', action: 'show', host: @site.hostname
url\u for返回第一个匹配的url,它应该属于哪个主机并不重要。因此,即使我放置了一个host:param,也不会使用host约束


您是否知道如何使用url_进行主机约束?

我的应用程序中也有相同的任务url\u for忽略主机参数。但我们可以通过以下方式在ApplicationController中创建其他路径帮助程序:

ApplicationController.rb

Frontend::Application.routes.draw do

  DynamicRoutes.load

end
class DynamicRoutes
  # dynamically loads the routes from settings into the routes.rb file
  # and adds a host constraint to just match with the current sites host
  # http://codeconnoisseur.org/ramblings/creating-dynamic-routes-at-runtime-in-rails-4
  def self.load
    if Site.table_exists?
      Frontend::Application.routes.draw do
        Site.includes(:setting).each do |site|
          site.routes.each do |route|
            # write the route with the host constraint
            constraints(:host => site.hostname) do
              case route[0]
              when :shop_show
                match "#{route[1]}", to: 'shops#show', via: [:get], as: "shop_show_#{site.id}"
              end
            end
          end
        end
      end
    end
  end

  # allows to reload the routing
  # e.g. when changes in route settings where made
  #
  def self.reload
    Rails.application.reload_routes!
  end
end
%w( shop_show ).each do |helper|
  helper_name = "#{helper}_path".to_sym
  helper_method helper_name
  define_method(helper_name) { |*args| send "#{helper}_#{site.id}_path", *args }
end
之后,您可以在视图中使用universal pathshop\u show\u path。当然,您应该根据主机/域动态分配站点变量