JDutil/contact_us

Using an optional locale scope makes the contact-us page unreachable

Closed this issue · 5 comments

briri commented

We just recently upgraded our app to Rails 4.2 and have begun using I18n localization. We updated our routes.rb file to:

scope "(:locale)", locale: /#{I18n.available_locales.join("|")}/ do
  resources :contacts, :controllers => {:contacts => 'contacts'}
  ...
end

When we try to access the page it results in a 'I18n::InvalidLocale - "contact-us" is not a valid locale:' error message.

I was able to track down the path that the ActionDispatch::Journey::Router was receiving and see that its 'contact-us/contacts'. This is causing the router to assume that the contact-us portion of the path is a locale.

Note: It doesn't matter if the 'resources :contacts' line is inside or outside the locale scope

I'm not sure I haven't actually made that configuration before, but the contact_us namespace should still work. Maybe try forcing the scope locale rather than making it optional to see if that makes a difference. I'm sure you've probably seen the guides, but maybe there is something you've missed http://guides.rubyonrails.org/i18n.html#setup-the-rails-application-for-internationalization it seems like configuration issue with how exactly your adding the scope/routes but I don't have a working example offhand sorry.

briri commented

I think the issue lies in the fact that this gem includes its own routing. The config/routes.rb file here translates to the following:

contacts POST          /contacts(.:format)             contact_us/contacts#create
new_contact GET      /contacts/new(.:format)     contact_us/contacts#new
contact_us GET        /contact-us(.:format)          contact_us/contacts#new

I cannot seem to override the gem's routes or wrap them within the optional locale scope in the initializer or within my app's config/routes.rb file and I think that's due to the order in which they are loaded by the Rails engine (I'm fairly new to the inner workings or Rails). I receive errors telling me that the 'contact_us' route already exists.

I was able to get around it by writing my own routes in my locale scope. The other ones (the gem supplied ones) are still present though.

scope "(:locale)", locale: /#{I18n.available_locales.join("|")}/ do
    post 'contact_form' => 'contacts', as: 'localized_contact_creation'
    get 'contact_form' => 'contacts#new', as: 'localized_contact_form'

    # all my other routes
end

It would be preferable I think if the routes were not supplied with the gem. you would of course have to add instructions on the Readme to instruct users on home to add it to their own config/routes.rb

That makes sense. You could probably add the scope to this gems routes if you'd like in a PR.

Otherwise then yea would probably need to remove how they are defined here, and make it more like devise or spree where the routes added to the application route file instead.

briri commented

I think it would be hard to support any of your existing users if you removed the routes completely. I'll fork, add the scope and then do a PR.
Thanks!

This was fixed by #58