Skip to content

Commit

Permalink
Dynamically set shoe select option.
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Polito committed Sep 2, 2020
1 parent 693f4f7 commit 7c47a33
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
9 changes: 6 additions & 3 deletions app/views/activities/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
<%= f.collection_select :difficulty, Activity.difficulties.keys.map{ |activity| [activity, activity.humanize] }, :first, :second, {}, class: "custom-select" %>
</div>
<div class="col-auto form-group">
<div id="shoes_select_wrapper">
<%= render partial: "activities/shoes_select", locals: { f: f } %>
</div>
<%= f.label :shoe_id, "Shoe" %>
<% if f.object.persisted? %>
<%= f.collection_select :shoe_id, current_user.shoes.alphabetized, :id, :name_with_miles, { prompt: true }, class: "custom-select" %>
<% else %>
<%= f.collection_select :shoe_id, current_user.shoes.available.alphabetized, :id, :name_with_miles, { prompt: true }, class: "custom-select" %>
<% end %>
<button type="button" class="btn btn-link" data-toggle="modal" data-target="#shoeFormModal">
Add Shoe
</button>
Expand Down
6 changes: 0 additions & 6 deletions app/views/activities/_shoes_select.html.erb

This file was deleted.

11 changes: 7 additions & 4 deletions app/views/shoes/create.js.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
var wrapper = document.querySelector("#shoes_select_wrapper");
var shoeSelectList = document.querySelector("#activity_shoe_id");

if(wrapper) {
wrapper.innerHTML = null;
wrapper.insertAdjacentHTML("afterbegin", <%= escape_javascript( render partial: "activities/shoes_select", locals: { f: @shoe } )%>);
if(shoeSelectList) {
var options = shoeSelectList.querySelectorAll("option");
options.forEach((option) => {
option.removeAttribute("selected")
});
shoeSelectList.querySelector("option").insertAdjacentHTML("afterend", "<option value='<%= @shoe.id %>' selected><%= @shoe.name_with_miles %></option>");
}

0 comments on commit 7c47a33

Please sign in to comment.