rails link_to标记以在rails中建立连接记录 [英] rails link_to tag to build join record in rails

查看:228
本文介绍了rails link_to标记以在rails中建立连接记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个允许用户向订单中添加产品的表单.

I'm trying to build a form that allows a user to add products to an order.

到目前为止,我的设置方式是用户将从2个下拉框中选择并输入1个文本字段 1-他们想要的产品 2-它的大小 3-他们想要的数量.

The way I have it setup so far is that a user will select from 2 dropdown boxes and type into 1 text field 1 - the product they want 2 - its size 3 - the quantity they want.

我希望做的是让用户单击link_to标记以将该商品添加"到他们的订单中.

What I hope to do is have the user click a link_to tag to "Add" this item to their order.

我当时想我可以通过ajax做到这一点,并在控制器中建立关联记录,并在请求返回时将其呈现在页面上.

I was thinking I could do this via ajax and build the associative record in my controller and have it render on the page when the request returns.

当用户完成他们的订单并点击提交后,我可以使用他们想要购买的产品来创建我的客户订单.

When the user is done with their order and hits submit I can create my Customer Order with the products they wish to buy.

我正确地解决了这个问题吗?

Am I approaching this correctly?

例如我的表格具有以下内容:

e.g. my form has the following:

<%= collection_select :order_line_item, :cake_id, Cake.order(:name), :id, :<%= 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", add_to_order_path, {method: :post, remote: true} %>

我正确地解决了这个问题吗?然后,我需要能够将上面的字段添加到ajax帖子中,以便可以使用相关值填充关联记录.

Am I approaching this correctly? I then need to be able to add the fields above to the ajax post so I can populate the associative record with the relevant values.

推荐答案

我正确地解决了这个问题吗?

Am I approaching this correctly?

我不了解正确" .但是,我可以想象一些替代方案.

I don't know about 'correctly'. But, I can imagine some alternatives.

以下是一些草图:

此方法假定已经保存了Order,以便您可以将Product与该顺序相关联.也许Order有一个status.

This approach assumes that the Order is already saved so that you can associate a Product with that order. Perhaps Order has a status.

  • 您可以将整个位(产品,尺寸,数量)包装成自己的形式(不嵌入订单中).
  • 使用remote: true通过js提交表单(如果您使用的是Rails 5,那么这可能是默认行为).
  • 当用户单击添加"时,您将在controller中收到作为parameters的字段值,您可以在其中将ProductOrder关联.
  • 然后,渲染回可以插入到DOM中的HTML blob(也许是订单行?)
  • 使用js插入blob并清除表单.
  • You could wrap that whole bit (product, size, quantity) in its own form (not embedded within your order form).
  • Have the form submit via js using remote: true (if you're using Rails 5, then this may be the default behavior).
  • When the user clicks on "Add", you will receive the field values as parameters in your controller where you can associate the Product with the Order.
  • Then, render back an HTML blob that can be inserted into the DOM (perhaps an order row?)
  • Use js to insert the blob and clear the form.
  • 您可以将整个部分(产品,尺寸,数量)保留为表格,而不是保留在表格的外部.
  • 将所有内容包装在一个div中.
  • 将链接转换为跨度或类似内容.
  • 将一个.on 'click'事件(我假设是jquery,您没有指定,所以我将使用它)运行到包装器.
  • 单击链接时,click事件将冒泡到包装器.
  • 让包装程序通过ajax提交字段值.
  • 按上述步骤进行.
  • You could leave that whole bit (product, size, quantity) as not a form and have it reside outside your form.
  • Wrap it all up in a div.
  • Convert that link into a span or something similar.
  • Attach an .on 'click' event (I'm assuming jquery, you don't specify, so I'm going to run with it) to the wrapper.
  • When the link is clicked, the click event will bubble up to the wrapper.
  • Have the wrapper submit the field values via ajax.
  • Proceed as above.

我不会真的推荐这种方法,因为在我看来,您基本上是在复制远程表单的功能.但是,有...

I wouldn't really recommend this approach as it seems to me that you're basically replicating the functionality of a remote form. But, there is...

这种方法不需要Order已经存在.

This approach does not require that the Order already exists.

  • 您可以在表单外部隐藏订单项行.
  • 您可以在另一个选项中按上述方式构建页面.
  • 现在,当用户单击添加"按钮时,克隆隐藏的订单项行.
  • 用适当的值填充克隆的订单项.
  • 将克隆的订单商品插入您的Order表单.
  • 当用户单击订单"或提交"或完成后单击的任何内容时,您将获得所有订单行作为字段集.
  • 处理订单行项目和表单. (有些人可能会建议accepts_nested_attributes_for,但我从不使用它.)
  • You could have a hidden order item row outside of your form.
  • You construct your page as above in Another Option.
  • Now, when the user clicks the "Add" button, clone the hidden order item row.
  • Fill in the cloned order item with the appropriate values.
  • Insert the cloned order item into your Order form.
  • When the user clicks "Order" or "Submit" or whatever they click when they're done, you'll get all of the order rows as field sets.
  • Process the order line items along with the form. (Some folks might suggest accepts_nested_attributes_for, but I never use that.)

我怀疑还有其他人.也许是变化.

I suspect there are others. Or perhaps variations.

这篇关于rails link_to标记以在rails中建立连接记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆