#17 Dependent Form Fields


Using unobtrusive javascript, we can hide certain fields on forms based on the selected values of other fields.
rails javascript view 4:19


Gemfile    gem 'jquery-turbolinks'
    gem 'dependent-fields-rails'
application.js    //= require jquery.turbolinks
    //= require underscore
    //= require dependent-fields

    $(document).ready(function() {
_form.html.erb    <%= f.input :extended_profile, id: 'user_extended_profile' %>
    <%= content_tag :div, class: 'js-dependent-fields', data: { 'checkbox-id': 'user_extended_profile', 'checkbox-value': 'true'} do %>
      <%= f.input :twitter_handle %>
      <%= f.input :facebook_handle %>
      <%= f.input :github_handle %>
    <% end %>
Schwad said over 1 year ago:

I'm bookmarking this episode the next time I want to enable or disable things in forms on an application... Currently I'd just whip in some JS with a few listeners that toggles things from enabled to disabled.... But I'd much more prefer this! Thanks!

Login to Comment