Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 范围:地点“;是否将我的主页重置为;“欢迎登机”;页_Ruby On Rails_Internationalization - Fatal编程技术网

Ruby on rails 范围:地点“;是否将我的主页重置为;“欢迎登机”;页

Ruby on rails 范围:地点“;是否将我的主页重置为;“欢迎登机”;页,ruby-on-rails,internationalization,Ruby On Rails,Internationalization,我正试图在我的网站上设置国际化选项,遵循这个railscast。当我在config.routes.rb中添加“scope”:locale“do”时,重新启动服务器并刷新localhost:3000,我得到了原始的“欢迎加入”模板 Config.routes.rb: Rails.application.routes.draw do scope ":locale" do get 'sessions/new' root 'welcome_pages#

我正试图在我的网站上设置国际化选项,遵循这个railscast。当我在config.routes.rb中添加“scope”:locale“do”时,重新启动服务器并刷新localhost:3000,我得到了原始的“欢迎加入”模板

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
应用程序\u 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
class ApplicationController

为什么会恢复到欢迎页面?

请尝试以下设置/:区域设置为可选:

#routes.rb
scope "(:locale)" do
  root 'welcome_pages#home'
  ...
end
然后

#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
#应用程序_controller.rb
类ApplicationController

它应该可以解决根路径重置的问题。

尝试以下设置/:区域设置可选:

#routes.rb
scope "(:locale)" do
  root 'welcome_pages#home'
  ...
end
然后

#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
#应用程序_controller.rb
类ApplicationController

它应该可以解决根路径重置的问题。

您可以在路由中使用:
范围(:locale)”,区域设置:/{I18n.available_locales.join(“|”)}/do
您可以在路由中使用:
范围(:locale)”,区域设置:/{I18n.available_locales.join(“}/do