user483040
user483040

Reputation:

Overriding a view in Spree with deface: how do I add inputs and their current values?

I'm trying to add an input to a form that already exists in one of the Spree admin views. Here's the override text (I cut out some cruft that was display centric and noisy):

new_html = "
  <div id='neato_new_field'>
    <p id='product_display_value'>
      <label for='display_value'>Display Value</label>
      <br>
      <input type='text' name='product[display_value]' size='30' id='product_display_value' value=''>
    </p>
  </div>"

Deface::Override.new(:virtual_path  => "spree/admin/products/_form",
                     :insert_after => "div.clearfix",
                     :text          => new_html,
                     :name          => "add_display_value")

What I need to know how to do is to set the value of the 'value' property on the input so that it picks up what is currently on that object when this view is rendered.

I tried putting value='#{@product.display_value}' in there but it complains that @product is nil. Can I even use interpolation like that to pick up the display_value? is this defacing done before the controller even tries to render or after?

I'm wondering now how to address this. Am I going to have to skim off the product id from the html that is already there, use it to load up the product and THEN put that value in there? I'm really hoping I'm missing something fundamental that makes this a trivial problem to solve :P That's kind of been the case with a lot of the Spree customizations--it's not clear how to do it at first but once I figure it out, it's easy as pie to implement my customizations...

thanks, jd

Upvotes: 3

Views: 2207

Answers (1)

user483040
user483040

Reputation:

The answer was fairly easy once I understood it.

Instead of using :text => to some random html markup, I pointed at a partial that as a result got all the neat behavior you get in any template view -- the variables in scope, in particular.

The end solution looks like this:

Deface::Override.new(:virtual_path => "spree/admin/products/_form",
                     :insert_after => "div.clearfix",
                     :partial      => "partials/admin",
                     :name         => "add_display_value")

and the partial:

<div>
  <%= f.field_container :display_value do %>
    <%= f.label :display_value, t(:display_value) %>
    <br>
    <%= f.text_field :display_value, :size => 30, :value => @product.display_value %>
  <% end %>
<div>

Upvotes: 5

Related Questions