Ruby on rails 3.2 理解一段铁路代码

Ruby on rails 3.2 理解一段铁路代码,ruby-on-rails-3.2,Ruby On Rails 3.2,我的客户端应该调用的服务的示例URL如下所示: http://www.example.com/providers/?query=eric&group_ids[]=1F23&group_ids[]=423&start=3&limit=20 已经编写的助手模块/方法如下所示,我应该将其用于我自己的客户端: def create_http_request(method, path, options = {}) # create/configure http h

我的客户端应该调用的服务的示例URL如下所示:

http://www.example.com/providers/?query=eric&group_ids[]=1F23&group_ids[]=423&start=3&limit=20
已经编写的助手模块/方法如下所示,我应该将其用于我自己的客户端:

def create_http_request(method, path, options = {})
  # create/configure http
  http = Net::HTTP.new(@base_uri.host, @base_uri.port)
  http.open_timeout = options[:open_timeout] || @open_timeout
  http.read_timeout = options[:read_timeout] || @read_timeout

  # create/configure request
  if method == :get
    request = Net::HTTP::Get.new(path)
  else
    raise ArgumentError, "Error: invalid http method: #{method}"
  end

  return http, request
end
在其他人编写的类似代码的其他部分中,我看到如下内容:为了调用create_http_request方法:

 def person_search(query, options = {})
  params = {
    query: query,
      group_ids: options[:group_ids],
    start: options[:page] || @default_page,
    limit: options[:page_size] || @default_page_size
  }

  pathSemantic = "/patients/?#{ params.to_param }"
  httpSemantic, requestSemantic = create_http_request(:get, pathSemantic, options)

主要是我不明白她和params在干什么,我们为什么要这么做?这是最好的办法吗

您是在询问to_param方法吗?它创建一个可以在URL中使用的参数字符串。您可以在此处阅读:

因此,在person搜索方法中,参数是基于查询、选项哈希中给定的值和存储在对象中的默认选项来构造的。它们附加到路径以创建pathSemantic字符串,然后将该字符串传递给create_http_请求方法

Re。参数的构造——它是一个散列,其中查询参数映射到:query键,中选项:group\u id的值映射到:group\u id键,选项:page的值映射到:start,等等

  params = {                                         # params is a hash
    query: query,                                    # the key query is mapped to the query method parameter (we got it from the call to the method)
    group_ids: options[:group_ids],                  # the key :group_ids is mapped to the value that we got in the options hash under the :group_id key (we also got the options as a parameter in teh call to the method)
    start: options[:page] || @default_page,          # the key :start is mapped to the value that we got in the options hash under the :page key. In case it is not set, we'll use the value in the instance variable @default_page
    limit: options[:page_size] || @default_page_size # the key :page_size is mapped to the value that we got in the options hash under the :page_size key. In case it is not set, we'll use the value in the instance variable @default_page_size
  }
如果您想知道x | | y表示法——这意味着如果没有设置x,则将使用y中的值(这是惯用形式中使用的快捷运算符“或”)

_param的方法适用于映射到数组的键:

{group_ids: ["asdf","dfgf","ert"]}.to_param
=> "group_ids%5B%5D=asdf&group_ids%5B%5D=dfgf&group_ids%5B%5D=ert"
哪个是URL编码

"group_ids[]=asdf&group_ids[]=dfgf&group_ids[]=ert"

嗯,主要是整个params部分。。这表示params={query:query,等等……谢谢,标记为答案,但还有最后一个问题:请注意,在我发布的URL服务(即example.com)中,查询可以有多个group_id[]……因此她在该方法中执行该操作的方式仍然适用于我?它应该适用。我在rails控制台中尝试过:{group_id:[“asdf”,“dfgf”,“ert”]}.to_param=>“group_ids%5B%5D=asdf&group_ids%5B%5D=dfgf&group_ids%5B%5D=ert”,这是“group_ids[]=asdf&group_ids[]=dfgf&group_ids[]=ert”的URL编码,在类似的代码中我也看到了类似的东西:Addressable::Template.new……我认为这是做同样事情的另一种方式,但有了这个可寻址的gem