I’m currently building a rails app where im trying to fetch data from different tables from my sql database and I need to put that fetched data (id, first_name, last_name) into an HTML Drop-down where the user is going to select between all possible choices, for exemple:
DROPDOWN Clients:
default: –SELECT–
1 Thomas Carrier
2 Michel Carrier
3 Yvon Dupuis
So if we take the first choice, 1 is the id of the first employee, and his first name (Thomas) and last name (Carrier)
I keep finding stuff about php but I cannot use php!
Thanks for the help
Advertisement
Answer
The easy answer
plain html
x
<select name="clients">
<option value="1">Thomas Carrier</option>
<option value="2">Michel Carrier</option>
</select>
or with ruby (https://guides.rubyonrails.org/layouts_and_rendering.html)
<select name="clients">
<% @clients.each do |client| %>
<option value="<%= client.id %>"><%= client.firstName %> <%= client.lastName %></option>
<% end %>
</select>
Also refer to Yaniv Iny’s answer to for a better more correct way