If there is a validation error, save action will redirect to the last known good location and set error message in a flash

This commit is contained in:
Alexey Verkhovsky 2005-01-17 23:17:28 +00:00
parent 5c8b738238
commit 73552b36a0
3 changed files with 54 additions and 19 deletions

View file

@ -7,6 +7,8 @@ class ApplicationController < ActionController::Base
# implements Instiki's legacy URLs # implements Instiki's legacy URLs
require 'url_rewriting_hack' require 'url_rewriting_hack'
after_filter :remember_location
# For injecting a different wiki model implementation. Intended for use in tests # For injecting a different wiki model implementation. Intended for use in tests
def self.wiki=(the_wiki) def self.wiki=(the_wiki)
# a global variable is used here because Rails reloads controller and model classes in the # a global variable is used here because Rails reloads controller and model classes in the
@ -15,13 +17,37 @@ class ApplicationController < ActionController::Base
$instiki_wiki_service = the_wiki $instiki_wiki_service = the_wiki
logger.debug("Wiki service: #{the_wiki.to_s}") logger.debug("Wiki service: #{the_wiki.to_s}")
end end
def self.wiki def self.wiki
$instiki_wiki_service $instiki_wiki_service
end end
protected
def wiki def wiki
$instiki_wiki_service $instiki_wiki_service
end end
@@REMEMBER_NOT = []
def remember_location
if @response.headers['Status'] == '200 OK'
@session[:return_to] = url_for unless @@REMEMBER_NOT.include? action_name
@session[:already_tried_index_as_fallback] = false
end
end
def return_to_last_remembered
# Forget the redirect location
redirect_target, @session[:return_to] = @session[:return_to], nil
# then try to redirect to it
if redirect_target.nil?
raise 'Cannot redirect to index' if @session[:already_tried_index_as_fallback]
@session[:already_tried_index_as_fallback] = true
redirect_to_url '/'
else
redirect_to_url(redirect_target)
end
end
end end

View file

@ -213,21 +213,26 @@ class WikiController < ApplicationController
def save def save
redirect_to :action => 'index' if @page_name.nil? redirect_to :action => 'index' if @page_name.nil?
if @web.pages[@page_name]
page = wiki.revise_page(
@web_name, @page_name, @params['content'], Time.now,
Author.new(@params['author'], remote_ip)
)
page.unlock
else
page = wiki.write_page(
@web_name, @page_name, @params['content'], Time.now,
Author.new(@params['author'], remote_ip)
)
end
cookies['author'] = @params['author'] cookies['author'] = @params['author']
redirect_show(@page_name)
begin
if @web.pages[@page_name]
page = wiki.revise_page(
@web_name, @page_name, @params['content'], Time.now,
Author.new(@params['author'], remote_ip)
)
page.unlock
else
page = wiki.write_page(
@web_name, @page_name, @params['content'], Time.now,
Author.new(@params['author'], remote_ip)
)
end
redirect_show(@page_name)
rescue Instiki::ValidationError => e
flash[:error] = e
return_to_last_remembered
end
end end
def show def show

View file

@ -543,13 +543,17 @@ class WikiControllerTest < Test::Unit::TestCase
def test_save_new_revision_identical_to_last def test_save_new_revision_identical_to_last
revisions_before = @home.revisions.size revisions_before = @home.revisions.size
assert_raise(Instiki::ValidationError) { r = process 'save', {'web' => 'wiki1', 'id' => 'HomePage',
process 'save', 'web' => 'wiki1', 'id' => 'HomePage',
'content' => @home.revisions.last.content.dup, 'content' => @home.revisions.last.content.dup,
'author' => 'SomeOtherAuthor' 'author' => 'SomeOtherAuthor'}, {:return_to => '/wiki1/show/HomePage'}
}
assert_redirect_url '/wiki1/show/HomePage'
assert_flash_has :error
assert r.flash[:error].kind_of?(Instiki::ValidationError)
revisions_after = @home.revisions.size revisions_after = @home.revisions.size
assert_equal revisions_before, revisions_after assert_equal revisions_before, revisions_after
end end