Skip to Main Content
Professional Ruby on Rails™
book

Professional Ruby on Rails™

by Noel Rappin
February 2008
Intermediate to advanced content levelIntermediate to advanced
479 pages
14h
English
Wrox
Content preview from Professional Ruby on Rails™

8.1. Revisiting the Past

In the Soups OnLine application as implemented so far, there are two separate uses of Ajax. It was used to edit ingredients in Chapter 4. I'd like to revisit those implementations to talk about some ways in which they could be improved.

To refresh your memory, the ingredient editing is located in app/views/recipes/show.html.rb, and consists of this link:

<div class="ingredient">
      <span id="ingredient_<%= ingredient.id %>">
        <%= h ingredient.display_string %>
      </span>
      <% if_is_current_user @recipe.user_id do %>
        <span class="subtle" id="edit_<%= ingredient.id %>">
          <%= link_to_remote "Edit",
              :url => remote_edit_recipe_ingredient_path(@recipe, ingredient),
              :method => :get,
              :update => "ingredient_#{ingredient.id}"%>
        </span>
      <% end %>
    </div>

On the server side, the controller is in app/controllers/ingredient_controller.rb, and just gets the appropriate ingredient:

def edit
    @ingredient = Ingredient.find(params[:id])
  end

  def remote_edit
    edit
  end

This displays a partial with the form to be entered:

<% remote_form_for(@ingredient, :url => remote_update_recipe_ingredient_path(@recipe, @ingredient), :update => "ingredient_#{@ingredient.id}") do |f| %> <table> <tr> <th class="subtle">Amount</th> <th class="subtle">Unit</th> <th class="subtle">Ingredient</th> <th class="subtle">Directions</th> </tr> <tr> <td><%= f.text_field :amount, :size => "5" %></td> <td><%= f.text_field :unit, :size => "10" %></td> <td><%= f.text_field :ingredient, :size => "25" %></td> <td><%= f.text_field ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Ruby on Rails® Bible

Ruby on Rails® Bible

Timothy Fisher
Rails 4 in Action

Rails 4 in Action

Yehuda Katz, Rebecca Skinner, Stephen Klabnik, Ryan Bigg

Publisher Resources

ISBN: 9780470223888Purchase book