Not RJS and Turbolinks

Episode #20 by Teacher's Avatar David Kimura


Using Javascript responses, we can make our Rails application have a quick and responsive Single Page Application feel to it.
rails javascript performance 8:55



# index.html.erb
    <h4>Listing Users</h4>
    <table class="table table-condensed table-hover">
          <th>First name</th>
          <th>Last name</th>
          <th>Email address</th>
      <tbody id='users'>
        <%= render @users %>

    <%= link_to 'New User', '#new_user', data: { toggle: 'modal' }, class: 'btn btn-primary' %>
    <div class="modal fade" id="new_user">
      <%= simple_form_for, remote: true do |f| %>
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4 class="modal-title">New User</h4>
          <div class="modal-body">
            <div class="form-inputs">
              <%= f.input :first_name %>
              <%= f.input :last_name %>
              <%= f.input :email_address %>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <%= f.button :submit, class: 'btn btn-primary' %>
      <% end %>

# _user.html.erb
    <tr id="<%= %>">
        <%= link_to icon('pencil'), edit_user_path(user) %>
        <%= link_to icon('trash'), user, method: :delete, remote: true, data: { confirm: 'Are you sure?' } %>
      <td><%= user.first_name %></td>
      <td><%= user.last_name %></td>
      <td><%= user.email_address %></td>
      <td><%= user.updated_at %></td>

# users_controller.rb
    def create
      @user =
      # redirect_to @user, notice: 'User was successfully created.'

    def destroy
      @user_id =
      # redirect_to users_url, notice: 'User was successfully destroyed.'

# create.js.erb
    $('#users').prepend('<%= j render @user %>');

# destroy.js.erb
    $('#<%= @user_id %>').slideUp();