Skip to content

Commit

Permalink
Redirect URL on successful submissions is configurable. Fixes #33
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Madere authored and Jeff Dutil committed Jul 23, 2013
1 parent fee8c23 commit fe1700c
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ config.require_name = true
config.require_subject = true
```

To redirect to a specific URL after a successful form submission:
```ruby
config.success_redirect = '/contact-success'
```

### Views

To copy the view files to `app/views/contact_us`, and customize them to suit your needs run:
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/contact_us/contacts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def create
@contact = ContactUs::Contact.new(params[:contact_us_contact])

if @contact.save
redirect_to('/', :notice => t('contact_us.notices.success'))
redirect_to(ContactUs.success_redirect || '/', :notice => t('contact_us.notices.success'))
else
flash[:error] = t('contact_us.notices.error')
render_new_page
Expand Down
3 changes: 3 additions & 0 deletions lib/contact_us.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ module ContactUs
# Formtastic or SimpleForm
mattr_accessor :form_gem

# URL after a successful submission
mattr_accessor :success_redirect

# Default way to setup ContactUs. Run rake contact_us:install to create
# a fresh initializer with all configuration values.
def self.setup
Expand Down
3 changes: 3 additions & 0 deletions lib/templates/contact_us.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@
# config.form_gem = 'formtastic'
config.form_gem = nil

# Configure the redirect URL after a successful submission
config.success_redirect = '/'

end
9 changes: 9 additions & 0 deletions spec/controllers/contact_us/contact_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,21 @@
end

it 'should redirect with success message if valid contact' do
ContactUs.success_redirect = nil
post :create, :contact_us_contact => { :email => 'test@test.com', :message => 'test' }
assigns(:contact).valid?.should eql(true)
flash[:notice].should eql('Contact email was successfully sent.')
response.should redirect_to('/')
end

it 'should redirect to custom URL with success message if valid contact' do
ContactUs.success_redirect = '/success'
post :create, :contact_us_contact => { :email => 'test@test.com', :message => 'test' }
assigns(:contact).valid?.should eql(true)
flash[:notice].should eql('Contact email was successfully sent.')
response.should redirect_to('/success')
end

it 'should render new with error message if invalid contact' do
post :create, :contact_us_contact => { :email => 'test@test.com', :message => '' }
assigns(:contact).valid?.should eql(false)
Expand Down
1 change: 1 addition & 0 deletions spec/lib/contact_us_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
ContactUs.mailer_to = nil
ContactUs.require_name = false
ContactUs.require_subject = false
ContactUs.success_redirect = nil
end

it "should be valid" do
Expand Down

0 comments on commit fe1700c

Please sign in to comment.