I'm making a simple website with a home page and a contact form. Now I'm trying to make it into 3 languages using the rails-i18n gem, and it was successful on the home page, but now I'm stuck with how to pass the locales from the home page to the contact page. The codes are as below and please let me know if you have any clues.
routes.rb (deleting 2 resources in the scope makes no difference)
Rails.application.routes.draw do
get 'contact' => 'inquiry#contact'
post 'contact/confirm' => 'inquiry#confirm'
post 'contact/thanks' => 'inquiry#thanks'
root 'static_pages#home'
match '/home', to: 'static_pages#home', via: 'get'
scope '(:locale)', locale: /#{I18n.available_locales.map(&:to_s).join('|')}/ do
resources :static_pages
resources :inquiry
end
end
application_controller.rb (deleting default_url_options makes no difference)
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :set_locale
def set_locale
I18n.locale = locale
end
def locale
@locale ||= params[:locale] ||= I18n.default_locale
end
def default_url_options(options={})
options.merge(locale: locale)
end
end
inquiry_controller.rb (when the contact is called, the locale becomes :en which is default)
class InquiryController < ApplicationController
def contact
@inquiry = Inquiry.new
render :action => 'contact'
end
def confirm
@inquiry = Inquiry.new(params[:inquiry])
if @inquiry.valid?
render :action => 'confirm'
else
render :action => 'contact'
end
end
def thanks
@inquiry = Inquiry.new(params[:inquiry])
InquiryMailer.received_email(@inquiry).deliver
render :action => 'thanks'
end
end
_dropdown.html.erb (the dropdown to change the language I added in the home page. It's not added in the contact page)
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Languages <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><%= link_to '英語/English', url_for(controller: controller.controller_name, action: controller.action_name, locale: 'en') %></li>
<li><%= link_to '中文/Mandarin', url_for(controller: controller.controller_name, action: controller.action_name, locale: 'ma') %></li>
<li><%= link_to '日本語/Japanese', url_for(controller: controller.controller_name, action: controller.action_name, locale: 'ja') %></li>
</ul>
</li>
console (I feel something's wrong...)
$ rake routes
Prefix Verb URI Pattern Controller#Action
contact GET /contact(.:format) inquiry#contact
contact_confirm POST /contact/confirm(.:format) inquiry#confirm
contact_thanks POST /contact/thanks(.:format) inquiry#thanks
root GET / static_pages#home
home GET /home(.:format) static_pages#home
static_pages GET (/:locale)/static_pages(.:format) static_pages#index {:locale=>/en|ma|ja/}
POST (/:locale)/static_pages(.:format) static_pages#create {:locale=>/en|ma|ja/}
new_static_page GET (/:locale)/static_pages/new(.:format) static_pages#new {:locale=>/en|ma|ja/}
edit_static_page GET (/:locale)/static_pages/:id/edit(.:format) static_pages#edit {:locale=>/en|ma|ja/}
static_page GET (/:locale)/static_pages/:id(.:format) static_pages#show {:locale=>/en|ma|ja/}
PATCH (/:locale)/static_pages/:id(.:format) static_pages#update {:locale=>/en|ma|ja/}
PUT (/:locale)/static_pages/:id(.:format) static_pages#update {:locale=>/en|ma|ja/}
DELETE (/:locale)/static_pages/:id(.:format) static_pages#destroy {:locale=>/en|ma|ja/}
inquiry_index GET (/:locale)/inquiry(.:format) inquiry#index {:locale=>/en|ma|ja/}
POST (/:locale)/inquiry(.:format) inquiry#create {:locale=>/en|ma|ja/}
new_inquiry GET (/:locale)/inquiry/new(.:format) inquiry#new {:locale=>/en|ma|ja/}
edit_inquiry GET (/:locale)/inquiry/:id/edit(.:format) inquiry#edit {:locale=>/en|ma|ja/}
inquiry GET (/:locale)/inquiry/:id(.:format) inquiry#show {:locale=>/en|ma|ja/}
PATCH (/:locale)/inquiry/:id(.:format) inquiry#update {:locale=>/en|ma|ja/}
PUT (/:locale)/inquiry/:id(.:format) inquiry#update {:locale=>/en|ma|ja/}
DELETE (/:locale)/inquiry/:id(.:format) inquiry#destroy {:locale=>/en|ma|ja/}
According to a blog page I referred to to set up all of these, the scope thing in routes.rb is supposed to enable access by /en or /ja, etc., it give routing errors. I feel like I didn't get the whole thing right... Any small advice would be appreciated!
Solved by changing the link to /contact from href="/contact" to <%= link_to t('view.contact'), url_for(controller: :inquiry, action: :contact) %>