0

I have a Sinatra app that I would like to have display a list of interactions between pairs of params. Right now I'm only allowing 2 selections but would like a solution that can allow as many as I want.

This code returns the desired interactions in the terminal but I cannot figure out how to display it on the page. Suggestions?

<p><%= array = params.values.permutation(2).to_a.each {|a| a.sort! }.uniq! %></p>
<div><%= array.each do |a| p Aed.interactions(a[0], a[1]).humanize end %>

1 Answer 1

2

Rendering an html page, injecting a value of a variable there is different from writing something to console. Putting a p(print/put/printf) statement inside a view file will not do anything, also putting it inside your route/controller will write it to console, rather than browser.

If you run below code and visit http://locahost:4567/hello/15/male/kensington (host and port may differ)

get '/hello/:age/:sex/:location' do 
 "Hello. I am a #{params[:sex]}. I am #{params[:age]} years old and I live in #{params[:location]}"
end

you'll see a text on browser window sth like,

Hello. I am a male. I am 15 years old and I live in kensington

If you have view files and want to pass some variables to that view,

get '/hello/:age/:sex/:location' do 
  @age = params[:age]
  @sex = params[:sex]
  @loc = params[:location]
  erb :hello 
end

sample hello.erb (let's assume you have such file),

<p>Below table holds some info about King Matt the First</p>

<table>
  <tr>
    <td>Age</td>
    <td>Sex</td>
    <td>Location</td>
  </tr>
  <tr>
    <td><%= @age %></td>
    <td><%= @sex %></td>
    <td><%= @loc %></td>
  </tr>
</table>

When you visit localhost:4567/11/male/warsaw

You'll see a page with that content.

enter image description here

When it comes to your code, use combination(2) instead of permutation(2).to_a {|a| a.sort! }.uniq!

2.3.1 :029 > a = [1,2,3].permutation(2).to_a.each {|a| a.sort! }.uniq!
 => [[1, 2], [1, 3], [2, 3]] 
2.3.1 :030 > b = [1,2,3].combination(2).to_a
 => [[1, 2], [1, 3], [2, 3]] 

Better do not use array as a variable name, but if you insist to use, your code should be written as to print something on browser window..

<div>
<% array.each do |a| %> 
<%= Aed.interactions(a[0], a[1]).humanize %> 
<% end %>
</div>

http://apidock.com/ruby/ERB

You should also consider to migrate your database queries inside a route/controller block.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the tip on using combination instead of permutation. I knew the difference between printing to console and rendering html. Turns out I was putting the = in the wrong place. I put it at the beginning of the each statement rather than in the block of that statement. As for database call, there is no database for this particular app. Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.