0

I'm writing a simple single page app. Right now the form.erb has 16 statically written forms that are all the same except their names and ids. I'd like to make it where form.erb only has one copy of the form but is yielded 16 times iterating the names and ids.

'/' gets routed to form.erb

get '/' do
    erb :form
end

the current layout.erb body is

<body>

<h2>Relay Setup</h2>
<h2><p id="CurrentTime"></p></h2>
<p><strong>Note:</strong> Set On/Off times for each relay respectively.</p>

<%= yield %>

</body>

and the current form.erb is

<form name="relay1" id="relay1" action="/" method="POST">
    <input name="relay1" type="hidden">
    <input name="label" id="label" type="text" value="<%= @label %>";">
    <input type="button" value="Add ON/OFF time" onClick="addInput('relay1');">
    <input type="submit">
</form>

I already tried a for loop in the router with no success. Do I just use more <%= yield %>'s and iterate the names in the router, or is there a flag to set in the layout? If not, is there some other, more proper way to accomplish this?

Thanks in advance...

iohzrd
  • 113
  • 2
  • 3

2 Answers2

0

is there some other, more proper way to accomplish this?

You can do this:

routes.rb:

get '/' do 

  @names_and_ids = [
    %w[nameA idA],
    %w[nameB idB],
    %w[nameC idC]
  ]

  erb :my_form

end

my_form.erb:

<% @names_and_ids.each do |(name, id)| %>

  <form name="<%= name %>" id="<%= id %>" action="/" method="POST">
    <input name="relay1" type="hidden">
    <input name="label" id="label" type="text" value="<%= @label %>";">
    <input type="button" value="Add ON/OFF time" onClick="addInput('relay1');">
    <input type="submit">
  </form>

<% end %> 

And using the following for views/layout.erb:

<!DOCTYPE html>
<head><title>Sinatra App</title></head>
<body>

<h2>Relay Setup</h2>
<h2><p id="CurrentTime"></p></h2>
<p><strong>Note:</strong> Set On/Off times for each relay respectively.</p>

<%= yield %>

</body>
</html>

will generate the following html:

<!DOCTYPE html>
<head><title>Sinatra App</title></head>
<body>

<h2>Relay Setup</h2>
<h2><p id="CurrentTime"></p></h2>
<p><strong>Note:</strong> Set On/Off times for each relay respectively.</p>


  <form name="nameA" id="idA" action="/" method="POST">
    <input name="relay1" type="hidden">
    <input name="label" id="label" type="text" value="";">
    <input type="button" value="Add ON/OFF time" onClick="addInput('relay1');">
    <input type="submit">
  </form>


  <form name="nameB" id="idB" action="/" method="POST">
    <input name="relay1" type="hidden">
    <input name="label" id="label" type="text" value="";">
    <input type="button" value="Add ON/OFF time" onClick="addInput('relay1');">
    <input type="submit">
  </form>


  <form name="nameC" id="idC" action="/" method="POST">
    <input name="relay1" type="hidden">
    <input name="label" id="label" type="text" value="";">
    <input type="button" value="Add ON/OFF time" onClick="addInput('relay1');">
    <input type="submit">
  </form>



</body>
</html>

which looks like this:

enter image description here

Do you really want multiple submit buttons?

7stud
  • 46,922
  • 14
  • 101
  • 127
  • Got it, thank you! I just needed to append a number to each name/id so I used <% (1..16).each do |i| %> at the beginning of form.erb and boom! – iohzrd Jul 13 '16 at 03:22
0

7stud's answer led me to this solution within my form.erb file. Thanks again!

<!DOCTYPE html>
<% (1..16).each do |i| %>
    <form method="POST" action="/" id="relay<%= i %>">
        <input name="relay<%= i %>" type="hidden">
        <input name="label" type="text" value="";">
        <input type="button" value="Add ON/OFF time" onClick="addInput('relay<%= i %>');">
        <input type="submit">
    </form>
<% end %>
iohzrd
  • 113
  • 2
  • 3