Datasheet
Chapter 1: Building Resources
35
Figure 1-4
To allow Ajax to work in your Rails application, you must load the relevant JavaScript files by includ-
ing the following line in the
app/views.layouts/recipes.html.erb file. Place the line in the
HTML header.
< %= javascript_include_tag :defaults % >
I find the best way to build in - place action like this is to build the action as a standalone first, and then
incorporate it into the view where needed. I ’ ve made the design decision to leave the existing edit and
update actions alone, and instead add new actions called
remote_edit and remote_update . Here are
the unit tests for them, in
ingredient_controller_test.rb :
def test_should_get_remote_edit
get :remote_edit, :id = > 1, :recipe_id = > 1
assert_select(“form[action=?]”,
remote_update_recipe_ingredient_path(1, 1)) do
assert_select “input[name *= amount]”
assert_select “input[name *= unit]”
assert_select “input[name *= ingredient]”
assert_select “input[name *= instruction]”
end
end
def test_should_remote_update_ingredient
put :remote_update, :id = > 1, :ingredient = > { :amount = > 2 },
:recipe_id = > 1
assert_equal “2 cups First Ingredient, Chopped”, @response.body
end
c01.indd 35c01.indd 35 1/30/08 4:02:31 PM1/30/08 4:02:31 PM