Masatoshi Nishiguchi

Updating a list with Ajax in Rails

View

...
<tbody>
  <% @household_items.each do |household_item| %>
    <tr id="household_item-<%= household_item.id %>">
      <td><%= household_item.name %></td>
      <td><%= household_item.volume %></td>
      <td><%= household_item.quantity %></td>
      <td><%= household_item.tag %></td>
      <td><%= household_item.description %></td>
      <td><%= link_to 'Show', household_item %></td>
      <td><%= link_to 'Edit', edit_household_item_path(household_item) %></td>
      <td><%= link_to 'Destroy', household_item,
                                 method: :delete,
                                 remote: true,
                                 data: { confirm: 'Are you sure?' } %></td>
    </tr>
  <% end %>
</tbody>
...

Create

app/controllers/household_items_controller.rb

  ...
  # POST /household_items
  # POST /household_items.json
  def create
    @household_item = HouseholdItem.new(household_item_params)

    respond_to do |format|
      if @household_item.save
        format.html { ... }
        format.js do
          # For ajax, use `flash.now`.
          flash.now[:success] = "HouseholdItem was successfully created."
          @household_item
        end
      else
        format.html { ... }
      end
    end
  end
  ...

app/views/household_items/create.js.erb

// Update the table.
var $tbody        = $("#household_items__table tbody")
var newRecordHTML = "<%= j render 'household_items/table_row', household_item: @household_item %>"
$tbody.prepend(newRecordHTML)

// Update flash.
$('#flash_box').html("<%= j render 'layouts/flash' %>")

Git rebase

Use case

One of the best ways to incorporate rebasing into your workflow is to clean up local, in-progress features.

$ git rebase -i HEAD~3

In case of conflicts

Relax and do not panic.

masa@Masas-Mac:~/blog (master)
$ git rebase -i HEAD~10
error: could not apply f8a87e6... Add layout to timeline in the About page

When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".

Could not apply f8a87e6a17d80b13048a7d2dfb90cfd77dd0066e... Add layout to timeline in the About page
masa@Masas-Mac:~/blog (master|REBASE-i 3/10)
$ git rebase --continue

Undoing git rebase

# Find the head commit of the branch:
$ git reflog
# Suppose the old commit was HEAD@{5} in the ref log:
git reset --hard HEAD@{5}