Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.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 如何添加单个方法来处理多个路由_Ruby On Rails_Ruby_Ruby On Rails 3_Routes_Ruby On Rails 3.2 - Fatal编程技术网

Ruby on rails 如何添加单个方法来处理多个路由

Ruby on rails 如何添加单个方法来处理多个路由,ruby-on-rails,ruby,ruby-on-rails-3,routes,ruby-on-rails-3.2,Ruby On Rails,Ruby,Ruby On Rails 3,Routes,Ruby On Rails 3.2,我的rails应用程序是一些RESTAPI服务的代理服务器,这意味着对rest服务器的所有请求都是通过我的rails应用程序路由的。我已经在routes.rb文件中定义了所有必需路由,并且在控制器中为每个路由编写了不同的方法。因此,我希望在控制器中使用一个方法来检查request.fullpath,并根据请求参数将其重定向到适当的rest服务调用,而不是针对每个路由使用不同的方法 这是我的路线图 get '/lookup/location/search', to: 'ticketing#lo

我的rails应用程序是一些RESTAPI服务的代理服务器,这意味着对rest服务器的所有请求都是通过我的rails应用程序路由的。我已经在routes.rb文件中定义了所有必需路由,并且在控制器中为每个路由编写了不同的方法。因此,我希望在控制器中使用一个方法来检查request.fullpath,并根据请求参数将其重定向到适当的rest服务调用,而不是针对每个路由使用不同的方法

这是我的路线图

  get '/lookup/location/search', to: 'ticketing#lookup_location_search'
  get '/lookup/company/search', to: 'ticketing#lookup_company_search'
  get '/lookup/assignmentGroup/search', to: 'ticketing#lookup_assignment_group_search'
  get '/lookup/ci/search', to: 'ticketing#lookup_ci_search'
  get '/lookup/user/search', to: 'ticketing#lookup_user_search'  

对于每个路由,控制器中都有一个单独的方法,而不是我想要一个单独的方法,它可以根据请求参数进一步调用正确的rest URL

# routes.rb
get '/lookup/*other_parts', to: 'ticketing#proxy_action' , format: false
# maybe with constraints
# get '/lookup/*other_parts', to: 'ticketing#proxy_action' , format: false,  constraints: { other_parts: /.../ }  

# within your controller
def proxy_action
  case params[:other_parts] 
    when 'location/search'
        ...
    when 'assignmentGroup/search'
       ...
    ...
    else
      ...
  end

我认为您可以使用通配符路由来解决以下问题:

# routes.rb
get '/lookup/*other_parts', to: 'ticketing#proxy_action' , format: false
# maybe with constraints
# get '/lookup/*other_parts', to: 'ticketing#proxy_action' , format: false,  constraints: { other_parts: /.../ }  

# within your controller
def proxy_action
  case params[:other_parts] 
    when 'location/search'
        ...
    when 'assignmentGroup/search'
       ...
    ...
    else
      ...
  end

@阿德里安:是的,你的权利。我是说这个案子。我确实更正了我的代码。所以,当您匹配switch语句中的其他部分时,如何匹配动态数据?e、 g-‘location/:location_id/search’@Sunny你能再详细解释一下吗?你的问题中没有提到这一点。我不知道我是否做对了。您仍然可以在“票务代理行动”中添加更多路线,如
get'/location/:location_id/*other_parts',格式:false
您只需确保最通用的规则最终出现。因此,在我的路由中,我保留了通用路由,将其重定向到单个控制器操作,如match'/ticketingService/*other_parts'=>'ticketing#proxy_action',via:[:get,:put,:post]在基于匹配路由使用交换机的控制器中,我呈现了一些模拟json,但现在我面临的问题是路由是否有一些动态数据。在我的交换机案例中,我如何匹配下面的路由,因为它包含location_id,并且是动态的“/location/:location_id/*other_part”,当两个路由相差太大时,我不会用一个动作来处理它。我将使用2个操作,并将公共逻辑提取到
private
方法。这大大简化了
case
语句,并且在我看来更面向对象。如果您想在一个操作中执行此操作,您可以先检查
参数[:location\u id]
是否存在,并处理所需的特殊逻辑,然后再继续执行
case
语句。@adrian是的,您的权利。我是说这个案子。我确实更正了我的代码。所以,当您匹配switch语句中的其他部分时,如何匹配动态数据?e、 g-‘location/:location_id/search’@Sunny你能再详细解释一下吗?你的问题中没有提到这一点。我不知道我是否做对了。您仍然可以在“票务代理行动”中添加更多路线,如
get'/location/:location_id/*other_parts',格式:false
您只需确保最通用的规则最终出现。因此,在我的路由中,我保留了通用路由,将其重定向到单个控制器操作,如match'/ticketingService/*other_parts'=>'ticketing#proxy_action',via:[:get,:put,:post]在基于匹配路由使用交换机的控制器中,我呈现了一些模拟json,但现在我面临的问题是路由是否有一些动态数据。在我的交换机案例中,我如何匹配下面的路由,因为它包含location_id,并且是动态的“/location/:location_id/*other_part”,当两个路由相差太大时,我不会用一个动作来处理它。我将使用2个操作,并将公共逻辑提取到
private
方法。这大大简化了
case
语句,并且在我看来更面向对象。如果您想在一个操作中执行此操作,您可以先检查
params[:location\u id]
是否存在,并在继续执行
case
语句之前处理所需的特殊逻辑。