1

I'm a complete novice in Ruby and Nanoc, but a project has been put in my lap. Basically, the code for the page brings back individual URLs for each item linking them to the manual. I'm trying to create a URL that will list all of the manuals in one search. Any help is appreciated.

Here's the code:

<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>
a_citizen
  • 11
  • 1
  • Let me see if I understand what you want. You have the variable *items* with URLs, and you want to extract some of these URLs from the *items* and... "I'm trying to create a URL that will list all of the manuals in one search.". This is weird, what you mean with that? Could explain better? And could also you give us some examples of URLs? – Pedro Gabriel Lima May 08 '18 at 16:41
  • Sorry, I know it's confusing. Right now the above code searches our catalog and extracts all SUV manuals and creates a list of Chevy, GMC, BMW, etc each item linking to its item-specific manual. I want to create a URL that grabs all of the car companies in a search. The link would look like http://www.my_url.comSearch/"Chevy"OR"GMC"OR"BMW" – a_citizen May 08 '18 at 18:45

1 Answers1

0

As you didn't specify what items is returning, I did an general example:

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.

Some considerations:

  • I don't know if you are gonna use this on view or controller, if will be in view, than wrap the code with the <% %> syntax.

  • About the URL format, you can find more choices of how to build it in: Passing array through URLs

  • When writing question on SO, please, put more work on that. You will help us find a quick answer to your question, and you, for wait less for an answer.

  • If you need something more specific, just ask and I can see if I can answer.

Pedro Gabriel Lima
  • 1,122
  • 1
  • 9
  • 24