Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[#26463195] Made the page form a remote form and handled the response…

… via jQuery. Also modified the pages_controller to be more like a regular controller, instead of rendering some special success template.
  • Loading branch information...
commit 9671e35aad5067f1a13bc038743dc9642bf3b841 1 parent d1956a5
@treybean treybean authored
View
12 app/assets/javascripts/tandem/pages.js.coffee
@@ -2,9 +2,7 @@
# All this logic will automatically be available in application.js.
$(document).ready ->
- $('.page_link').colorbox
- onCleanup: ->
- location.reload()
+ $('.page_link').colorbox()
if $('#tandem_page_links').length > 0
$('body').addClass('tandem-admin-bar')
@@ -24,3 +22,11 @@ $(document).ready ->
height: '90%'
open: true
return false
+
+ $(document).on 'ajax:success', 'form.tandem-page-form', (event, content, status, jqXHR) ->
+ returnedForm = $('form.tandem-page-form', content)
+
+ if returnedForm.find('#error_explanation').length > 0
+ $('form.tandem-page-form').replaceWith returnedForm
+ else
+ window.location.reload()
View
6 app/controllers/tandem/pages_controller.rb
@@ -51,7 +51,7 @@ def edit
def create
respond_to do |format|
if @page.save
- format.html { render action: "success", notice: 'Page was successfully created.' }
+ format.html { redirect_to @page, notice: 'Page was successfully created.' }
format.json { render json: @page, status: :created, location: @page }
else
format.html { render action: "new" }
@@ -65,8 +65,8 @@ def create
def update
respond_to do |format|
if @page.update_attributes(params[:page])
- format.html { render action: "success", notice: 'Page was successfully updated.' }
- format.json { head :ok }
+ format.html { redirect_to @page, notice: 'Page was successfully updated.' }
+ format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @page.errors, status: :unprocessable_entity }
View
2  app/helpers/tandem/pages_helper.rb
@@ -220,7 +220,7 @@ def valid_templates
private
def invalid_templates
- ['show', 'edit', 'index', 'new', 'success']
+ ['show', 'edit', 'index', 'new']
end
def valid_custom_template?(template_name)
View
2  app/views/tandem/pages/_form.html.slim
@@ -1,6 +1,6 @@
// This form controls New Page, Edit Page
#tandem-editor
- = form_for @page do |f|
+ = form_for @page, remote: true, html: { class: 'tandem-page-form' } do |f|
-if @page.errors.any?
#error_explanation
h2 = "#{pluralize(@page.errors.count, "error")} prohibited this page from being saved:"
View
3  app/views/tandem/pages/success.html.slim
@@ -1,3 +0,0 @@
-p#notice = notice
-
-#success_data style='display:none' resource_url="#{page_path(@page)}"
View
10 spec/controllers/tandem/pages_controller_spec.rb
@@ -25,7 +25,7 @@ module Tandem
# Page. As you add validations to Page, be sure to
# update the return value of this method accordingly.
def valid_attributes
- Factory.attributes_for(:tandem_page)
+ @valid_attributes ||= Factory.attributes_for(:tandem_page)
end
describe "GET index" do
@@ -143,9 +143,9 @@ def valid_attributes
assigns(:page).should be_persisted
end
- it "render the 'success' template" do
+ it "should redirect to the page" do
post :create, :page => valid_attributes
- response.should render_template("success")
+ response.should redirect_to("/tandem/pages/#{valid_attributes[:slug]}")
end
end
@@ -184,10 +184,10 @@ def valid_attributes
assigns(:page).should eq(page)
end
- it "render the 'success' template" do
+ it "should redirect to the page" do
page = Factory(:tandem_page)
put :update, :id => page.to_param, :page => valid_attributes
- response.should render_template("success")
+ response.should redirect_to("/tandem/pages/#{valid_attributes[:slug]}")
end
end
View
23 spec/requests/pages_spec.rb
@@ -1,6 +1,8 @@
require 'spec_helper'
describe "Pages" do
+ include RequestHelpers::PagesHelpers
+
before(:each) do
Factory(:tandem_page)
end
@@ -10,4 +12,25 @@
get '/tandem'
end
end
+
+ describe 'creating a new page' do
+ context 'by submitting a valid form' do
+ it 'shows the page form and creates a new page', :js => true do
+ when_I_go_to_the_home_page_and_click_new_page
+ i_should_see_the_new_page_form_in_a_modalbox
+ when_I_fill_out_the_page_form_and_hit_save
+ then_I_should_see_the_new_page_in_the_page_listings
+ and_I_should_be_able_to_visit_the_new_page
+ end
+ end
+
+ context 'by submitting an invalid form' do
+ it 'should show the errors on the form within the colorbox', :js => true do
+ when_I_go_to_the_home_page_and_click_new_page
+ i_should_see_the_new_page_form_in_a_modalbox
+ when_I_submit_a_blank_page_form
+ then_I_should_see_errors_in_page_form
+ end
+ end
+ end
end
View
51 spec/support/request_helpers/pages_helpers.rb
@@ -0,0 +1,51 @@
+module RequestHelpers
+ module PagesHelpers
+ def when_I_go_to_the_home_page_and_click_new_page
+ visit root_path
+ click_link 'New Page'
+ end
+
+ def i_should_see_the_new_page_form_in_a_modalbox
+ sleep 1
+ within '#colorbox' do
+ page.should have_selector('form.tandem-page-form')
+ end
+ end
+
+ def when_I_fill_out_the_page_form_and_hit_save
+ within 'form.tandem-page-form' do
+ fill_in 'Title', :with => 'cherries'
+ fill_in 'Link label', :with => 'cherries'
+ fill_in 'Slug', :with => 'cherries'
+ click_button 'Save'
+ end
+ end
+
+ def then_I_should_see_the_new_page_in_the_page_listings
+ visit '/tandem/pages'
+ page.should have_content('cherries')
+ end
+
+ def and_I_should_be_able_to_visit_the_new_page
+ visit '/tandem/pages/cherries'
+ page.should have_content('Sample Content')
+ end
+
+ def when_I_submit_a_blank_page_form
+ within 'form.tandem-page-form' do
+ click_button 'Save'
+ end
+ end
+
+ def then_I_should_see_errors_in_page_form
+ within '#colorbox' do
+ page.should have_selector('form.tandem-page-form')
+
+ within 'form.tandem-page-form' do
+ page.should have_selector('#error_explanation')
+ end
+ end
+ end
+ end
+end
+

2 comments on commit 9671e35

@treybean
Owner

I toyed with the idea of having the form submit and receive JSON, but then it seemed like the front-end was having to do a bit of special handling to craft error elements and what not, when simply looking at the returned HTML already had that done. I'm not sure what the best practice is on this one, aside from a backbone view. :)

@evizitei
Collaborator

I think the "best practice" varies heavily with the preferred style of the developer. I myself prefer HTML responses as it requires very little work to just inject them in somewhere. I've never been a huge fan of the "js.erb" response at the other end of the spectrum. I've used JSON since it's realy small, especially on high volume stuff. For this, I like the HTML.

Please sign in to comment.
Something went wrong with that request. Please try again.