1

我完全是 Ruby 和 Nanoc 的新手,但是我已经完成了一个项目。基本上,该页面的代码为将它们链接到手册的每个项目带回了单独的 URL。我正在尝试创建一个 URL,该 URL 将在一次搜索中列出所有手册。任何帮助表示赞赏。

这是代码:

<div>
  <%
    manuals = @items.find_all('/manuals/autos/*')
      .select {|item| item[:tag] == 'suv' }
      .sort_by {|item| item[:search] }

    manuals.each_slice((manuals.size / 4.0).ceil).each do |manuals_column|
  %>
    <div>
      <% manual_column.each do |manual| %>
        <div>
          <a href="<%= app_url "/SearchManual/\"#{manual[:search]}\"" %>">
            <%= manual[:search] %>
          </a>
        </div>
      <% end %>
    </div>
  <% end %>
</div>
4

1 回答 1

0

由于您没有指定要返回的项目,我做了一个一般示例:

require 'uri'

# let suppose that your items query has the follow output
manuals = ["Chevy", "GMC", "BMW"]

# build the url base
url = "www.mycars.com/search/?list_of_cars="

# build the parameter that will be passed by the url
manuals.each do |car|
    url += car + ","
end

# remove the last added comma
url.slice!(-1)

your_new_url = URI::encode(url)

# www.mycars.com/?list_of_cars=Chevy,GMC,BMW

# In your controller, you will be able to get the parameter with
# URI::decode(params[:list_of_cars]) and it will be a string:
# "Chevy,GMC,BMW".split(',') method to get each value.

一些考虑:

  • 我不知道您是否要在视图或控制器上使用它,是否会在视图中,而不是用<% %>语法包装代码。

  • 关于 URL 格式,您可以找到更多关于如何构建它的选择: Passing array through URLs

  • 在写关于 SO 的问题时,请在这方面做更多的工作。您将帮助我们快速找到您问题的答案,而您则无需等待答案。

  • 如果您需要更具体的内容,请询问,我可以看看是否可以回答。

于 2018-05-08T19:46:59.890 回答