0

I have a basic form in rails:

<% form_for shipping_object do |f| %>
<%= f.text_field :address1, :placeholder => "Address Line 1*", :class => "forms" %><br/>
<%= f.text_field :address2, :placeholder => "Address Line 2", :class => "forms" %>
<%= f.collection_select :state_id, @states.where(country_id: f.object.country_id).order(:name), :id, :name, include_blank: '-- select state --'  %>
<%= f.text_field :state_name, placeholder: 'State', class: 'forms' %>
<%= f.text_field :zipcode, :placeholder => "Zip Code*", :class => "forms" %>
<% end %>

I'd like to have a UI/UX checkbox element that a user can click and have all the form areas populated with pre-defined variables, but I'm not sure how I would do that. I'm assuming it's done with .attr methods in Javascript, but I'm not sure how?

Alex
  • 95
  • 1
  • 8
  • Have you looked into setting a JS listener for the checkbox, then [filling the form with the data using jQuery](https://stackoverflow.com/a/10545605/3878329)? – Peter Zhu Aug 03 '18 at 17:46
  • That's exactly what I was looking for! thanks – Alex Aug 03 '18 at 18:15

1 Answers1

0

Here's a formal answer for people who stumble upon this in the future:

Pass the data from Rails to the JavaScript while the page is being rendered and have a JS listener that listens to when the checkbox is clicked. When this listener is fired, fill in the form using jQuery.

Peter Zhu
  • 421
  • 6
  • 16