jsonrubydatasetmiddlemansnipcart

Replace double-quotes string by simple-quote


I'm using Middleman and Snipcart to develop an e-commerce project. I need to use multi-currency, the Snipcart documentation says I just have to set in my buy-button a data-item-price with a JSON array like {"usd": 20, "eur": 25}. There's others dataset to put on the buy-button, so I decided to make a helper.

This helper return exactly what I want, but the data-item-price is between double quotes and not between single quotes. For the moment, the code is understand as data-item-price="{"usd":20,"eur":22}" and not as data-item-price='{"usd": 20, "eur": 22}'

I need to transform <button data-item-price="{"usd":20,"eur":22}">Buy</button> by this <button data-item-price='{"usd":20,"eur":22}'>Buy</button>.

Here's the snipcart (buy) button helper:

def snipcart_button (p, text)
  args = {
    "class" => "snipcart-add-item",
    "data-item-id" => p.id,
    "data-item-price" => p.price.to_json,
    "data-item-name" => p[locale].name,
    "data-item-max-quantity" => p.max_quantity,
    "data-item-url" => snipcart_product_url(p),
    "data-item-image" => p.image
  }

  content_tag :button, args do
    text
  end
end

Solution

  • What you are looking for is probably not double quotes to single quotes, but this helper: https://apidock.com/rails/ActionView/Helpers/JavaScriptHelper/escape_javascript

    This will escape double and single quotes.

    "data-item-price" => j(p.price.to_json),