I have an Ant show page that displays detailed information about various types of ants. On that page there are two drop downs, one for environment: [indoor, outdoor], and one for diet: [sugar, fat, protein].
When you select a param from each it renders out a product page depending on the params. However, some combinations result in nil, like a carpenter ant doesn't have a product associated with indoor, sugar.
I'm trying to get the dropdowns to populate based on whether or not a combination is nil. If someone selects indoor I'd like sugar to not appear in the next drop down if that combo doesn't exist.
So far I have I have two methods creating json arrays for only the available items:
def food_sources
food_sources = Ant.find(params[:ant_id]).product_recommendations.where(environment: params[:environment]).map(&:diet)
render json: food_sources.to_json
end
def environments
environments = Ant.find(params[:ant_id]).product_recommendations.where(diet: params[:diet]).map(&:environment)
render json: environments.to_json
end
For example, if input
http://localhost:3000/ants/27/food_sources?environment=indoor
into the browser it returns
["sugar","protein"]
bc for the ant with an id of 27 only has two options for outdoor indoor diets, instead of the possible three.
My question is how do I pass this array into my rails dropdown if someone picked the above combination?
EDIT
= form_tag ant_product_recommendations_path(@ant.id), id: 'select_box', method: :get do |form|
%h3 What food source are they currently feeding on?
= select_tag :environment, options_for_select([["Select One"], "Indoor", "Outdoor"])
%h3
Locate the nest by following the ants back from their food source.
%br
Where is the nest located?
= select_tag :diet, options_for_select([["Select One"], "Sugar", "Fat", "Protein"])
= submit_tag "Find Solutions", class: 'submit button'
= link_to 'Go Back', '/', class: 'go_back'