Skip to content

Commit

Permalink
AJAX forms submit correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Morrison committed Mar 14, 2010
1 parent 6943c83 commit f98a1e1
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class UsersController < ApplicationController
skip_before_filter :verify_authenticity_token

def new
render
end

def create
respond_to do |format|
format.js do
name = params[:user][:name]
render :js => "$('output_area').innerHTML = 'Whats up, #{name}?';"
end
end
end
end
2 changes: 2 additions & 0 deletions app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module UsersHelper
end
2 changes: 2 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<%= javascript_include_tag :defaults %>
<%= yield %>
9 changes: 9 additions & 0 deletions app/views/names/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<% remote_form_for(@user, :update => 'output_area' ) do |form| %>
<%= form.label(:name) %>
<%= form.text_field(:name) %>
<%= form.submit_tag %>
<% end %>

<div id="output_area">
</div>
9 changes: 9 additions & 0 deletions app/views/users/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<% remote_form_for(:user, :url => users_path, :method => 'POST') do |form| %>
<%= form.label(:name) %>
<%= form.text_field(:name) %>
<%= form.submit %>
<% end %>

<div id="output_area">
</div>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ActionController::Routing::Routes.draw do |map|
map.resources :users
map.root :controller => 'homes', :action => 'index'
end
9 changes: 9 additions & 0 deletions features/js.feature
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,12 @@
body.appendChild(para);
"""
Then I should see "Hello from javascript"

Scenario: AJAX forms submit correctly
Given I am on the AJAX form page
When I fill in "Name" with "Jason"
And I press "Save changes"
Then I should see "Whats up, Jason?"

Scenario: link_to_remote witih POST works
Scenario: link_to_remote witih DELETE works
3 changes: 3 additions & 0 deletions features/support/paths.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ def path_to(page_name)

when /the home\s?page/
'/'

when /the AJAX form page/
new_user_path

# Add more mappings here.
# Here is an example that pulls values out of the Regexp:
Expand Down
8 changes: 8 additions & 0 deletions test/functional/users_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'test_helper'

class UsersControllerTest < ActionController::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end
4 changes: 4 additions & 0 deletions test/unit/helpers/users_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'test_helper'

class UsersHelperTest < ActionView::TestCase
end

0 comments on commit f98a1e1

Please sign in to comment.