Ruby on rails Rails-为嵌套表单增量生成联接记录
这是我最初提出的问题,但下面的思考为场景添加了更多细节: 我希望有一个表单,允许我增量地将关联记录添加到一个对象中,这也必须在表单提交时创建 模型设置(-<是一对多) 客户-Ruby on rails Rails-为嵌套表单增量生成联接记录,ruby-on-rails,ajax,dynamic,model-associations,jointable,Ruby On Rails,Ajax,Dynamic,Model Associations,Jointable,这是我最初提出的问题,但下面的思考为场景添加了更多细节: 我希望有一个表单,允许我增量地将关联记录添加到一个对象中,这也必须在表单提交时创建 模型设置(- 警惕“出了问题” 我想出了一个解决方案——但我仍然不确定这是否是“Rails方式” 到目前为止,我不得不依赖我的应用程序所具有的依赖性/局限性 即: 1) 客户一次只能下一个订单(也没有关联用户的登录/身份验证) 了解了这一点,并了解了rails如何通过关联构建id和名称,我现在正在动态构建生成联接表元素所需的html e、 g (注意,
def custom_order
puts "custom_order called"
@cakeTypes = CakeType.all
@cakes = Cake.all
@customer = Customer.new
@customer.customer_orders.build
end
客户一次只能提交一个订单,这就是我构建customer\u orders对象的原因。我可以在订单行项目中包含一个构建器,但这是动态的,我的问题是关于什么的
我在下面的表格中有一个链接,它有add to order,这调用了一点javascript,在用户从下拉列表中选择他们想要的产品、数量等后,它会向控制器发送一个ajax请求,以获取订单的相关细节
我是否朝着正确的方向“增量构建”(正如我所描述的)我希望一次创建所有对象的嵌套表单的连接记录?如何通过“轨道方式”做到这一点
我的表单:添加html注释以进一步描述我正在尝试做的事情:
<%= form_with(model: customer, local: true, url: "/custom_order") do |form| %>
<!-- customer form fields, name, phone, email-->
<%= render 'customer_fields', f: form %>
<div class="field">
<%= form.fields_for :customer_orders do |builder| %>
<%= builder.label :date_needed %>
<%= builder.date_select :date_needed %>
<br />
<%= builder.label :comments %>
<%= builder.text_area :comments %>
<% end %>
</div>
<!-- order form -->
<div class="field">
<!-- We want to display to user the cake sizes available depending on the cake they select -->
<!-- the cake and size they select should inform the cake_price_id which is a hidden field -->
<!-- {'data-order-line-item-cake' => "#{line_item_builder.options[:child_index]}"} -->
<%= collection_select :order_line_item, :cake_id, Cake.order(:name), :id, :name %>
<%= grouped_collection_select :order_line_item, :cake_size_id, Cake.all, :cake_sizes, :name, :id, :name %>
<%= label_tag :quantity %>
<%= text_field_tag :quantity %>
<%= link_to "Add to order", {}, id: 'add_to_order', remote: true %> <!-- , method: :post, remote: true %> -->
<!-- A "customer" on this page will only every create one order -->
<!-- so each item should field should have -->
<!--customer[customer_orders_attributes][0][date_needed] -->
<!--customer[customer_orders_attributes][0][comments] -->
<!--customer[customer_orders_attributes][0][total] HIDDEN!-->
<div id="order-items">
<% if false %>
<%= text_field_tag "customer[customer_orders_attributes][0][order_line_items_attributes][1][customer_order_id]", "", disabled: true%>
<% end %>
<!-- <input type="text" name="customer[customer_orders_attributes][0][order_line_items_attributes][1][customer_order_id]" id="customer_customer_orders_attributes_0_order_line_items_attributes_1_customer_order_id" value="" disabled="disabled"> -->
<!-- names -->
<!--customer[customer_orders_attributes][0][order_line_items_attributes][0..N][customer_order_id] -->
<!--customer[customer_orders_attributes][0][order_line_items_attributes][0..N][cake_price_id] -->
<!--customer[customer_orders_attributes][0][order_line_items_attributes][0..N][quantity] -->
</div>
<div class="actions">
<%= submit_tag "Submit Order" %>
</div>
<% end %>
我的咖啡脚本——现在有点粗糙,因为我还在学习和尝试。我想知道我目前所做的是否正确,如果可能的话,我如何动态地构建连接记录
$ ->
$('#add_to_order').click ->
console.log("add to order!")
myData =
cake_id: $('#order_line_item_cake_id :selected').val()
cake_size_id: $('#order_line_item_cake_size_id :selected').val()
quantity: $('#quantity').val()
$.ajax
url: "/build_order_item"
type: 'GET'
dataType: 'json'
data: (myData)
success: (data) ->
console.log(data)
#$('#order-items').append("<h6>#{data.quantity} - #{data.size} of #{data.cake} - #{data.price}</h6>")
$('#order-items').append("<input type=\"hidden\" name=\"jims_cake_price_id\" id=\"jims_cake_price_id\" value=#{data.cake_price_id}><br />")
error: ->
alert "Something went wrong"
$->
$(“#将#添加到#订单”)。单击->
log(“添加到订单!”)
我的数据=
蛋糕id:$(“#订单行(项目)蛋糕id:选定”).val()
蛋糕大小id:$(“#订单行(项目)蛋糕大小id:selected”).val()
数量:$('数量').val()
$.ajax
url:“/build\u order\u item”
键入:“获取”
数据类型:“json”
数据:(myData)
成功:(数据)->
console.log(数据)
#$(“#订单项”).append(“#{data.quantity}-#{data.size}of#{data.cake}-#{data.price}”)
$(“#订单项”)。追加(
)
错误:->
警惕“出了问题”
我想出了一个解决方案——但我仍然不确定这是否是“Rails方式”
到目前为止,我不得不依赖我的应用程序所具有的依赖性/局限性
即:
1) 客户一次只能下一个订单(也没有关联用户的登录/身份验证)
了解了这一点,并了解了rails如何通过关联构建id和名称,我现在正在动态构建生成联接表元素所需的html
e、 g
(注意,第一个“0”是硬编码的,因为我知道这将是第一个订单)
第二个“0”是通过javascript生成的。这是删除元素时我重新排序的索引
i、 e
如果客户添加3个产品,则索引将为0,1,2-如果他们删除索引0,则必须更新id以反映0和1
上述解决方案面临的问题:
当一个元素被删除时,我必须对元素的id重新排序——这还不错,因为对于已排序的项目数,它们将始终为0..N-1
我真的很想知道的是,我是否可以说是“出轨了”。。。我在上面做的对吗?还是完全可以接受?或者我只是担心用“Rails方式”去做,然后跟着做——如果它对我有用的话
非常感谢您的回复
<input type="hidden" name="customer[customer_orders_attributes][0][order_line_items_attributes][0][cake_price_id]" value="1">