Skip to content

Commit

Permalink
Merge pull request #58 from briri/master
Browse files Browse the repository at this point in the history
Added the optional locale scope to the config/routes.rb file. Added a…
  • Loading branch information
JDutil committed Aug 8, 2016
2 parents 10c2e2e + 3e8cac4 commit bd3d155
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ To redirect to a specific URL after a successful form submission:
config.success_redirect = '/contact-success'
```

If you're using I18n localization and would like to have the locale be a part of your paths
For example: /en/contact-us OR /fr/contact-us OR /en-UK/contact-us
```ruby
config.localize_routes = true
```

### Views

To copy the view files to `app/views/contact_us`, and customize them to suit your needs run:
Expand All @@ -86,6 +92,9 @@ bundle exec rake contact_us:copy_locales

Please feel free to submit your own locales so that other users will hopefully find this gem more useful in your language.

If you would like to include the locale in your paths (e.g. /en/contact-us), set the localize_routes
parameter to true in your initializer (see the configuration section above)

### Formtastic

In order to use a Formtastic compatible template to hook into your custom form styles configure `config/initializers/contact_us.rb`:
Expand Down
23 changes: 19 additions & 4 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
Rails.application.routes.draw do
resources :contacts,
:controller => 'contact_us/contacts',
:only => [:new, :create]
get 'contact-us' => 'contact_us/contacts#new', :as => :contact_us
# Place the contact_us routes within the optional locale scope
# If the I18n gem is installed and the localize_routes variable has
# been set to true in the application's initializer file
if defined?(I18n) && ContactUs.localize_routes
scope "(:locale)", locale: /#{I18n.available_locales.join("|")}/ do
resources :contacts,
controller: "contact_us/contacts",
only: [:new, :create]

get "contact-us" => "contact_us/contacts#new", as: :contact_us
end

else
resources :contacts,
controller: "contact_us/contacts",
only: [:new, :create]

get "contact-us" => "contact_us/contacts#new", as: :contact_us
end
end
4 changes: 4 additions & 0 deletions lib/contact_us.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ module ContactUs
mattr_accessor :parent_mailer
@@parent_mailer = "ActionMailer::Base"

# allows for a locale to appear in the path
# (e.g. /fr/contact-us OR /en/contact-us)
mattr_accessor :localize_routes

# Default way to setup ContactUs. Run rake contact_us:install to create
# a fresh initializer with all configuration values.
def self.setup
Expand Down

0 comments on commit bd3d155

Please sign in to comment.