Linking to search results with Ruby

63 Views Asked by At

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>
1

There are 1 best solutions below

2
Pedro Gabriel Lima On

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.