首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Ruby链接到搜索结果

使用Ruby链接到搜索结果
EN

Stack Overflow用户
提问于 2018-05-09 00:01:06
回答 1查看 57关注 0票数 1

我完全是Ruby和Nanoc的新手,但是有一个项目已经放在我的腿上了。基本上,页面的代码为每个项目返回单独的URL,将它们链接到手册。我正在尝试创建一个URL,它将在一次搜索中列出所有手册。任何帮助都是非常感谢的。

代码如下:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

发布于 2018-05-09 03:47:00

因为您没有指定返回哪些项,所以我做了一个通用的示例:

代码语言:javascript
复制
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
  • When
  • on,所以,请在上面多花点功夫。您将帮助我们快速找到您的问题的答案,并且您可以更少地等待答案。
  • 如果您需要更具体的东西,只需提问,我就可以看看我是否可以回答。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50237862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档