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 ...
Get Professional Ruby on Rails™ now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.