Skip to content

Commit 8b0708b

Browse files
committed
select vs combobox
1 parent e33c89f commit 8b0708b

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

app/views/posts/_form.html.erb

+29-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
</div>
1818

1919
<div>
20-
<%= form.label :status, style: "display: block" %>
2120
<%
2221
value = if post.status.present?
2322
# Ideally we should be able to pass the value as a hash or any object that responds to `value` & `display`, or any other API that you think it's good
@@ -29,16 +28,41 @@
2928
# Parse JSON response
3029
data = JSON.parse(response)
3130

32-
{
33-
value: data["id"],
34-
display: data["title"]
35-
}
31+
[data["id"], data["title"]]
3632
else
3733
nil
3834
end
35+
36+
# Only for select, combobox fetch options async
37+
# Fetch products from an API
38+
url = URI("https://dummyjson.com/products/search?q=#{params[:q]}")
39+
40+
# # Make the HTTP request
41+
response = Net::HTTP.get(url)
42+
43+
# # Parse JSON response
44+
data = JSON.parse(response)
45+
46+
options = data["products"].map do |item|
47+
[item["title"], item["id"]]
48+
end
3949
%>
50+
51+
<% if value.present? %>
52+
</br>
53+
Value is -> <%= value %>
54+
</br>
55+
</br>
56+
<% end %>
57+
58+
<%= form.label :status, style: "display: block" %>
4059
<%= form.combobox :status, hotwire_combobox_options_path, value: value %>
4160
</div>
61+
</br>
62+
<%= form.label :status_select, style: "display: block" %>
63+
<%= form.select :status_select, options_for_select(options, selected: value), include_blank: true %>
64+
</br>
65+
</br>
4266

4367
<div>
4468
<%= form.submit %>

0 commit comments

Comments
 (0)