Я использую pg_search и у меня проблемы с локализацией. У меня есть модель shop_item с атрибутами title1, title2, description1, description2. Зависит от языка. Я использую комбинацию title1 + description1 или title2 + description2. В контроллере приложений у меня есть метод, который устанавливает локаль:
class ApplicationController < ActionController::Base
before_action :set_locale
private
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
end
ShopItemsController:
def index
if params[:search].present?
@shop_items = ShopItem.search_for_items(params[:search]).paginate(:page => params[:page], :per_page => 32).order('created_at DESC')
else
@shop_items = ShopItem.all.paginate(:page => params[:page], :per_page => 32).order('created_at DESC')
end
end
И в моем shop_items/index.html.erb у меня есть:
<%= form_tag shop_items_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search], autofocus: true, type: "text", placeholder: 'search for messages'%>
<%= submit_tag "search", type: "submit"%>
<% end %>
</br>
<% if current_page?(locale: :ua)%>
<div class="row">
<% @shop_items.each do |si| %>
<div class="col m3 ">
<div class="card hoverable">
<div class="card-image">
<%= image_tag si.shop_image.url(:large) %>
</div>
<div class="card-content"-->
<span class="card-title truncate"><td><%= si.title1 %></td></span>
<p><%= t(:price)%>: <%= si.price%></p>
</div>
<div class="card-action">
<%= link_to t(:details), shop_item_path(si) %>
<%= link_to t(:edit), edit_shop_item_path(si) if current_user.present? and current_user.admin? %>
<%= link_to t(:destroy), si, method: :delete, data: { confirm: 'Are you sure?' } if current_user.present? and current_user.admin? %>
</div>
</div>
</div>
<% end %>
</div>
<% end %>
Если я иду в shop_items_path, я вижу в своем браузере следующий URL-адрес по умолчанию:
http://localhost:3000/shop_items?locale=ua
Я вижу все мои shop_items, но если я удалю локаль из URL-адреса, например:
http://localhost:3000/shop_items
Все элементы исчезают. Когда я использую поиск, у меня есть URL-адрес:
http://localhost:3000/shop_items?utf8=%E2%9C%93&search=item&commit=search
Как видите, локаль отсутствует, в результате после поиска я вижу любые shop_items.
Мой вопрос: как сохранить текущую локаль и включить ее в поисковый запрос?
Спасибо за любое решение!
ОБНОВЛЕНО: Rails.application.routes.draw do root :to => 'shop_items#index' ресурсы :shopping_contacts ресурсы :cart_items
resources :shopping_carts do
#resources :contact_infos
resources :shopping_contacts
resources :cart_items
resources :cart_confirms
end
resources :shop_items do
resources :cart_items
end
resources :contact_us
resources :contacts, only: [:new, :create, :edit, :update, :index, :destroy]
get 'password_resets/new'
get 'password_resets/edit'
get 'sessions/new'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
get '/signup', to: 'users#new'
post '/signup', to: 'users#create'
resources :users
resources :main_shots
конец