Question on data-controller-target

What would be the rails text_field helper equivalent of this?

<input type="text" data-total-target="qty" class= "form-control" name="qty" id="qty" data-action="change->total#settotal">

I wasn’t sure how to specify the data-total-target with the text_field helper.

Thanks.

Is the field really named just qty? That would mean to me that it was made with the text_field_tag helper rather than the text_field helper, since the latter always binds the form field to an instance of your model, and the fields are named something like your_model_name[qty] rather than just qty.

f.text_field :qty, class: 'form_control', 
                         data: {
                           action: 'change->total#settotal',
                           total: { target: 'qty' }
                         }

…ought to do it. If you find that the data-total-target doesn’t come out kebab-case like you want it with my nested syntax, you can also do data: { action: '...', 'total-target': 'qty' } and that will guaranteed work. The data: key always concatenates to it’s direct children with a - rather than a _, but I don’t know off-hand if it always does that with all of its descendants.

It is also entirely okay (if a little more duplicative and long-hand) to just make up a symbol for each of the data tags you want to add, like 'data-foo-bar': 'baz', 'data-blarg-boo: 'blooey'. That also works and you end up with the same attributes.

Walter

Thanks!

This worked!

<%= form.text_field :qty, class: 'form-control', data: {action: 'change->total#settotal', 'total-target': 'qty'} %>