【问题标题】:Save a Rendered table to a hidden field in Rails 4将渲染表保存到 Rails 4 中的隐藏字段
【发布时间】:2015-09-17 19:46:23
【问题描述】:

我正在使用 acts_as_shopping_cart gem 为我的 rails 4 应用程序构建购物车功能。 gem 有助于呈现一张漂亮的表格,其中包含订单的价格、数量和总数。我想将此作为 html 收据保存在我的交易表中的文本列下。

这些是我要保存的视图:

shopping_carts/show.html.erb

<h1>Shopping Cart</h1>

<%= render :partial => 'shopping_cart_item', :collection => @shopping_cart.shopping_cart_items %>

<div>
  <div><b>SubTotal:</b><%= number_to_currency @shopping_cart.subtotal %></div>
  <div><b>Taxes:</b><%= number_to_currency @shopping_cart.taxes %></div>
  <div><b>Total:</b><%= number_to_currency @shopping_cart.total %></div>

/shopping_carts/_shopping_cart_item.html.erb

<div>
  <div><%= shopping_cart_item.item.name %></div>
  <div><%= shopping_cart_item.item.price %></div>
</div>

如何在我的 Transaction.receipt 字段中将其保存为一个 html 块?

【问题讨论】:

    标签: ruby-on-rails forms ruby-on-rails-4 rendering


    【解决方案1】:

    我强烈建议不要这样做。将其保存为 hash/json 对象可能会更好?

    不过,您可以使用 render_to_string 将渲染的输出保存到一个字符串中,然后您可以将其保存到您的数据库中。

    http://api.rubyonrails.org/classes/AbstractController/Rendering.html#method-i-render_to_string

    【讨论】:

    • 如果我把它保存为一个json对象,我在哪里可以存储它? (我是新手,没有使用过 json 对象)
    • 您可以将其保存在同一个文本字段中...您的目标是什么?为什么我建议不要在你的数据库中保存 html 标记,如果你必须修改标记会发生什么?向标记添加一个类?然后,您必须更新所有收据。如果它只是数据的 JSON,那么您只需在一个地方修改它,即视图。
    • 我只想在收据上记录一个记录,以便在查看去年的交易时能够调出。我一直在玩弄转换为pdf然后保存为aws的想法……但我认为必须有一种更简单的方法。交易仅存储金额/支票信息/等...而不是逐项购买。
    • 我建议将金额、支票信息等存储在事务表中,每个表都有相应的列。然后您可以拉起 Transaction.last 或 Transaction.where("amount > 100").count 等。这将为您提供更大的灵活性,尤其是在您使用固定数量的列时(假设您使用的是关系型数据库)。
    猜你喜欢
    • 2018-06-27
    • 2021-02-24
    • 2015-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-28
    • 2013-04-23
    • 1970-01-01
    相关资源
    最近更新 更多