简体   繁体   English

以 simple_form 呈现动态 select 列表

[英]Rendering a dynamic select list in simple_form

I am relatively new in JS/Ajax and I am note sure how to render dynamic options, based on previous inserted data in the same (simple_)form.我在 JS/Ajax 方面相对较新,我注意到如何根据先前以相同 (simple_) 形式插入的数据来呈现动态选项。

Context: Via the application a bike shop chain ('chains') can rent out背景:通过该应用程序,自行车连锁店(“连锁店”)可以出租

  • bikes with names such as "bike 1" or "yellow bike" ('bikes'),名称如“自行车 1”或“黄色自行车”(“自行车”)的自行车,
  • by picking out a bike type such as mountainbike, city bike etc. ('bike_types) and通过选择自行车类型,如山地自行车、城市自行车等('bike_types)和
  • bike options, such as helmets etc. ('bike_options')自行车选项,例如头盔等('bike_options')
  • which are dependent on the individual bike store ('bike_stores')这取决于单个自行车商店('bike_stores')
  • this rental of the bikes & options will all be captured in an order ('orders')自行车和选项的租赁将全部按订单(“订单”)
  • the relationship between orders and bikes is many-to-many, therefore - I created a table to bridge this ('order_bikes')订单和自行车之间的关系是多对多的,因此 - 我创建了一个表来连接它('order_bikes')

Outstanding question: I am able to get the bike_type_id via JS.悬而未决的问题:我可以通过 JS 获得 bike_type_id。 How can I use this id of the bike_type to show the bikes belonging to that bike_type in the same form?如何使用这个 bike_type 的 id 以相同的形式显示属于该 bike_type 的自行车?

As mentioned, unfortunately I am not really familiar with the usage of JS/Ajax in a rails apps, so it would be really appreciated and helpful if paths of the files could be added where the suggested code should be written (eg app/javascript/components/order.js etc.)如前所述,不幸的是,我并不真正熟悉 JS/Ajax 在 Rails 应用程序中的使用,因此如果可以在应编写建议代码的位置添加文件路径(例如 app/javascript/组件/order.js 等)

Final notes:最后注意事项:

  • Before the rental process, the chain owner first created his/her (i) bike_stores, (ii) bike_types, (iii) bikes and (iv) bike_options, this part of the application is working.在租赁过程之前,连锁店所有者首先创建了他/她的 (i)bike_stores、(ii)bike_types、(iii)bikes 和 (iv)bike_options,这部分应用程序正在运行。 Therefore, he/she only needs to select bike_types/bikes/options out of the existing inventory previously created.因此,他/她只需要将 select bike_types/bikes/options 从先前创建的现有库存中取出。
  • I limit the scope of the question by leaving out the bike_options, this was mainly to provide some context in order to understand the db schema build up.我通过省略bike_options来限制问题的scope,这主要是为了提供一些上下文以了解数据库模式的构建。

models楷模

class Order < ApplicationRecord
  belongs_to :bike_store
  has_many :bike_types, through: :bike_store
  has_many :order_bikes, inverse_of: :order, dependent: :destroy
  accepts_nested_attributes_for :order_bikes, allow_destroy: true
end


class OrderBike < ApplicationRecord
  belongs_to :bike
  belongs_to :order
  accepts_nested_attributes_for :bike
end


class Bike < ApplicationRecord
  belongs_to :bike_type
  validates :name, presence: true
  has_many :order_bikes
  has_many :orders, through: :order_bikes
end


class BikeType < ApplicationRecord
  belongs_to :bike_store
  has_many :bikes, dependent: :destroy
  accepts_nested_attributes_for :bikes, allow_destroy: true
  has_many :bike_options, dependent: :destroy
  accepts_nested_attributes_for :bike_options, allow_destroy: true
  validates :name, :bike_count, presence: true
end

class BikeStore < ApplicationRecord
  has_many :bike_types, dependent: :destroy
  has_many :orders, dependent: :destroy
end

order controller订购 controller

class OrdersController < ApplicationController

  def new
    @bike_store = BikeStore.find(params[:bike_store_id])
    @order = Order.new
    @order.order_bikes.build
    @bike_type_list = @bike_store.bike_types
  end

  def create
    @order = Order.new(order_params)
    @bike_store = BikeStore.find(params[:bike_store_id])
    @order.bike_store = @bike_store
    @order.save
    redirect_to root_path
  end

private
  def order_params
    params.require(:order).permit(:arrival, :departure,
      order_bikes_attributes: [:id, :bike_id,:bike_quantity, :_destroy,
        bikes_attributes: [:id, :name,
          bike_types_attributes: [:id, :name]]])
  end
end

views/orders/new.html.erb意见/订单/new.html.erb

<%= simple_form_for [@bike_store, @order] do |f|%>

  <%= f.simple_fields_for :order_bikes do |order_bike| %>
    <%= order_bike.input :bike_quantity %>

      <%= order_bike.simple_fields_for :bikes do |bike| %>

        #fist a bike_type will be classified, see below
        <%= bike.select :bike_type_id, options_for_select(@bike_type_list.collect{|type|[type.name, type.id]}) %>
      <% end %>

      #then a dropdown of bikes belonging to above chose bike_type need to be displayed below.
      <%= order_bike.association :bike, collection [bike_type.bikes] %>
  <% end %>

  <%= f.input :arrival %>
  <%= f.input :departure %>
  <%= f.submit %>
<% end %>

<script>
  // return id bike_type
  function selectType(){
  const bikeType = document.getElementById("order_order_bikes_attributes_0_bikes_bike_type_id").value;
  }
</script>

Wow figuring this out was painful/took some time.... For the people looking at this, I found a working answer, hope it saves you some time.哇,弄清楚这一点很痛苦/花了一些时间......对于看到这个的人,我找到了一个可行的答案,希望它可以节省你一些时间。

I'm pretty sure it is not the most elegant solution, so please feel free to optimize.我很确定这不是最优雅的解决方案,所以请随时优化。 I did it with Ajax, please find the solution below:我是用 Ajax 做的,请在下面找到解决方案:

In the view在视图中

<%= simple_form_for [@bike_store, @order] do |f|%>

  <%= f.simple_fields_for :order_bikes do |order_bike| %>
    <%= order_bike.input :bike_quantity %>

      <%= order_bike.simple_fields_for :bikes do |bike| %>

        #fist a bike_type will be classified, see below
        <%= bike.select :bike_type_id, options_for_select(@bike_type_list.collect{|type|[type.name, type.id]}) %>
      <% end %>

      #then a dropdown of bikes belonging to above chose bike_type need to be displayed below.
      <%= order_bike.association :bike, collection [bike_type.bikes] %>
  <% end %>

  <%= f.input :arrival %>
  <%= f.input :departure %>
  <%= f.submit %>
<% end %>

<script>

$(document).on("change", "#order_order_bikes_attributes_0_bikes_bike_type_id", function(){
    var bike_type = $(this).val();

   $.ajax({
    url: "/bike_stores/<%= @bike_store.id %>/orders/new",
    method: "GET",
    dataType: "json",
    data: {bike_type: bike_type},
    error: function (xhr, status, error) {
      console.error('AJAX Error: ' + status + error);
    },
    success: function (response) {
      console.log(response);
      var bikes = response["bikes"];
      $("#order_order_bikes_attributes_0_bikes_bike_type_id").empty();

      $("#order_order_bikes_attributes_0_bike_id").append('<option>Select bike</option>');
      for(var i=0; i< bikes.length; i++){
        $("#order_order_bikes_attributes_0_bike_id").append('<option value="' + bikes[i]["id"] + '">' + bikes[i]["name"] + '</option>');
      }
    }
  });
});
</script>

order controller订购 controller

  def new
    @bike_store = BikeStore.find(params[:bike_store_id])
    @order = Order.new
    @order.order_bikes.build
    @bike_type_list = @bike_store.bike_types

    if params[:bike_type].present?
      @bikess = BikeType.find(params[:bike_type]).bikes
    end
    if request.xhr?
      respond_to do |format|
        format.json {
        render json: {bikes: @bikes}
      }
    end
  end
end
  end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM