Dependent Form Fields

#17 Dependent Form Fields
9/2/2015

Summary

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

Summary

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

    $(document).ready(function() {
      DependentFields.bind()
    });
_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 %>
7865030?v=3&s=64
Schwad said 6 months 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