范围“:locale”将我的主页重置为“Welcome Aboard”页面

时间:2015-11-25 11:32:03

标签: ruby-on-rails internationalization

我正在尝试按照此跟踪广播https://www.youtube.com/watch?v=kBdZ9_yGLjg在我的网站上设置国际化选项。当我在config.routes.rb中添加“scope”:locale“do”时,重启服务器并刷新localhost:3000,我得到原来的“Welcome Aboard”模板。

Config.routes.rb:

Rails.application.routes.draw do
  scope ":locale" do  

    get 'sessions/new'

    root             'welcome_pages#home'
    get 'help'    => 'welcome_pages#help'
    get 'about'   => 'welcome_pages#about'
    get 'contact' => 'welcome_pages#contact'

    get 'signup'  => 'operators#new'
    get 'operators/new'
    resources :operators

    get 'vts/new'
    resources :vts

    get    'login'   => 'sessions#new'
    post   'login'   => 'sessions#create'
    delete 'logout'  => 'sessions#destroy'

  end 
end

application_controller.rb

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
  include SessionsHelper
  before_filter :set_locale

  private 

  def set_locale
    I18n.locale = params[:locale] if params[:locale].present?
  end

  def default_url_options(options = {})
    {locale: I18n.locale}
  end 

end

为什么要回到欢迎页面?

2 个答案:

答案 0 :(得分:0)

尝试以下方法将/:locale设置为可选:

#application_controller.rb
class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_action :set_locale

  private

    def set_locale
      I18n.locale = params[:locale] || I18n.default_locale
    end

    def default_url_options(options = {})
      { locale: I18n.locale }.merge options
    end

end

然后

{{1}}

它应该通过root_path重置修复你的问题。

答案 1 :(得分:0)

您可以在路线中使用:

<!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery-1.11.3.js"></script> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> </head> <body> <h1>Books</h1> <h2>Book Price</h2> <form action="" method="post"> <select name="book" id="book"> <option value="XML Developer's Guide">XML Developer's Guide</option> <option value="Midnight Rain">Midnight Rain</option> <option value="Oberon's Legacy">Oberon's Legacy</option> <option value="Splish Splash">Splish Splash</option> <option value="Creepy Crawlies">Creepy Crawlies</option> </select> Select a book to see the price <br> <br> <input type="button" id="find" value="Find!"> </form> <div id="forPrice"></div> <h2>Author Names and Book Titles</h2> <div id="forBookInformation"></div> <h2>Book Publish Date</h2> <div id="forPublishDate"></div> </body> </html>