[英]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背景:通过该应用程序,自行车连锁店(“连锁店”)可以出租
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:最后注意事项:
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.