spree / deface

Rails plugin that allows you to customize ERB views in a Rails application without editing the underlying view.
MIT License
517 stars 128 forks source link

String interpolation is not working in deface erb. #223

Closed MIbtehajNasar closed 2 years ago

MIbtehajNasar commented 2 years ago

String interpolation is not working in deface override erb. I think It consider it as a local variable that should be define inside the file.

<div data-hook="admin_product_form_sold_for_credits">
      <%= f.field_container :sold_for_credits do %>
        <label>
          <%= t('spree.admin.sold_for_credits') %>
          <%= f.select :sold_for_credits, options_for_select(Spree::Product.sold_for_credits.map {|k, v| [Spree::Product.human_attribute_name("sold_for_credits.#{k}"), k]}, selected: f.object.sold_for_credits), {include_blank: false}, class: 'custom-select fullwidth' %>
        </label>
      <% end %>
    </div>

getting this error

undefined local variable or methodk' for main:Object

Hint: k is probably misspelled.`

spaghetticode commented 1 year ago

This is happening to me as well, @MIbtehajNasar I see you closed the issue but I was curious to understand if you were able to fix it or what?

One workaround is to put the template in a separate erb file rather than inside the override.

I'm adding my broken code for further context:

template = <<ERB
  <div class="field products_rule_products">
    <%= label_tag "#{param_prefix}_product_ids_string", t('spree.product_rule.choose_products') %>
    <%= hidden_field_tag "#{param_prefix}[product_ids_string]", promotion_rule.product_ids.join(","), class: "product_picker fullwidth" %>
  </div>
ERB

Deface::Override.new(
  name: "filter_available_products",
  virtual_path: "spree/admin/promotions/rules/_product",
  original: "",
  replace: ".products_rule_products",
  template: template
)

Which results in NameError undefined local variable or method `param_prefix' for main:Object