相关疑难解决方法(0)

如何使用json数组填充Rails下拉菜单

我有一个Ant显示页面,显示有关各种类型蚂蚁的详细信息.在那个页面上有两个下降,一个用于环境:[室内,室外],一个用于饮食:[糖,脂肪,蛋白质].

当您从每个参数中选择一个参数时,它会根据参数显示一个产品页面.然而,一些组合导致零,如木匠蚂蚁没有与室内糖相关的产品.

我试图根据组合是否为零来获取下拉列表.如果有人选择室内,我会喜欢糖,如果该组合不存在,则不会出现在下一个下拉菜单中.

到目前为止,我有两种方法只为可用的项创建json数组:

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
Run Code Online (Sandbox Code Playgroud)

例如,如果输入

http://localhost:3000/ants/27/food_sources?environment=indoor
Run Code Online (Sandbox Code Playgroud)

进入浏览器返回

["sugar","protein"]
Run Code Online (Sandbox Code Playgroud)

bc对于id为27的蚂蚁,只有两种户外室内饮食选择,而不是可能的三种.

我的问题是,如果有人选择了上述组合,我如何将此数组传递到我的rails下拉列表?

编辑

= 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"], …
Run Code Online (Sandbox Code Playgroud)

javascript jquery json ruby-on-rails

2
推荐指数
1
解决办法
2619
查看次数

标签 统计

javascript ×1

jquery ×1

json ×1

ruby-on-rails ×1