2017-06-09 2 views
0

私はpg_searchを使用しており、ローカリゼーションに問題があります。 私は、title1、title2、description1、description2の属性を持つshop_itemモデルを持っています。私はロケールを設定する方法を持っている私は、アプリケーションのコントローラでTITLE1 +説明1、またはTITLE2 +記述2 の組み合わせを使用していた言語からの依存:pg_searchとローカライズ

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 /インデックス内

.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 %> 

は私が私のブラウザのデフォルトとして、次のURLでご覧shop_items_pathするつもりです場合:

http://localhost:3000/shop_items?locale=ua 

私はすべての私のshop_itemsを見ることができますが、私は次のように、URLからロケールを削除していた場合:

http://localhost:3000/shop_items?utf8=%E2%9C%93&search=item&commit=search 

http://localhost:3000/shop_items 

すべての項目は、私は、検索を使用していたとき、私はのようなURLを持っている 消えています

ロケールが見つからないことがわかったので、結果として検索後にshop_itemsが表示されます。

私の質問は、現在のロケールを保存して検索クエリに含める方法です。

ありがとうございました!

更新: Rails.application.routes.drawは ルートを実行します。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 

[OK]を、更新のために考えて終わり

+1

あなたのルートを投稿できますか? –

+0

@RonanLouarn確かに更新されました。ありがとう – anndrew78

答えて

1

=> 'shop_items#インデックス' 資源におそらく、あなたはこれらの手順に従うべきです。

最初のネストは 'scope'(:locale) '、locale:/ en | es/do:'のようになります。

Rails.application.routes.draw do 
scope '(:locale)', locale: /en|es/ do 


    root :to => 'shop_items#index' 
    resources :shopping_contacts 
    resources :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 
end 
end 

第二ステップはapplication_controller.rbでそのように、このメソッドを追加します。

before_action :set_locale 

    def set_locale 
    I18n.locale = params.fetch(:locale, I18n.default_locale).to_sym 
    end 

    def default_url_options 
    { locale: I18n.locale == I18n.default_locale ? nil : I18n.locale } 
    end 

最後のステップは、あなたがapplication.rbでdefautlローカルに設定されている:?

config.i18n.default_locale = :en 

は私にあなたのフィードバックを、 がんばろう。

+0

あなたは私の一日を作った。魔法のように!十億回ありがとう – anndrew78

+0

問題はありません;)、楽しい –