在 Rails 上的 React 中获取未经允许的参数::locale

问题描述 投票:0回答:1

我收到以下错误。

    error
'  ↳ app/controllers/concerns/set_locale.rb:35:in `switch_locale'
Unpermitted parameter: :locale'

这是我的set_locale.rb

module SetLocale
  extend ActiveSupport::Concern

  included do
    around_action :switch_locale
    helper_method :current_locale
    helper_method :switch_locale_urls
  end

  def current_locale
    extract_locale.presence || I18n.default_locale
  end

  def switch_locale_urls
    locale_regex = %r{^/(#{I18n.available_locales.join('|')})/}i

    I18n.available_locales.index_with do |locale|
      if request.method == 'GET' && original_fullpath.match(locale_regex)
        original_fullpath.gsub(locale_regex, "/#{locale}/")
      else
        root_path(locale: locale)
      end
    end.to_h
  end

  protected def original_fullpath
    request.original_fullpath
  end

  private def switch_locale(&action)
    locale = extract_locale || I18n.default_locale
    response.set_header('Content-Language', locale.to_s)
    ActiveRecord::Base.connection.execute("alter session set nls_sort = 'THAI_M'") #this is line 35 where its causing error
    I18n.with_locale(locale, &action)
  end

  private def extract_locale
    parsed_locale = params[:locale]
    I18n.available_locales.map(&:to_s).include?(parsed_locale) ? parsed_locale : nil
  end

  private def default_url_options
    { locale: I18n.locale }
  end
end

下面是我的ApplicationController.rb

class ApplicationController < ActionController::Base
  include BasicAuthProtection
  include CurrentUserSession
  include ErrorPages
  include SetLocale
  include Pagination

  helper VimeoHelper
  helper SVGHelper
  helper ChunksHelper

  protect_from_forgery
  per_request_react_rails_prerenderer

  before_action :permit_additional_params
  before_action :add_ssr_variables, :store_location

  
  def store_location
    unless request.path.include?('session')
      session[:previous_url] = request.fullpath
    end
  end
  
  # Permit the locale parameter globally
  protected def permit_locale_param
    params.permit(:locale)
  end
  
  # rubocop:disable Rails/OutputSafety
  protected def add_ssr_variables
    react_rails_prerenderer
    .context
      .exec("global.I18N_LOCALE = #{I18n.locale.to_json.html_safe}")
      react_rails_prerenderer
      .context
      .exec("global.FORM_AUTHENTICITY_TOKEN = #{form_authenticity_token.to_json.html_safe}")
    end
    # rubocop:enable all
    private
  
    def permit_additional_params
      # Permit the locale and other custom params
      params.permit(:locale, :page, :per_page, :sort, filters: { cuisine: [], dish_type: [], main_ingredient: [] })
    end
  end

我尝试添加

params.permit
但仍然不起作用。最近 db 发生了变化,但 .env 我已经正确添加了所有字段。由于数据库已更改,我面临这个问题。以下是使用过的包的版本

Rails:v6.0.3.6,Ruby:v2.7.2,Bundler:2.1.4

如果有人知道解决方法,请提供帮助

ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 rubygems
1个回答
0
投票

params[:locale]
是一个不允许的参数,因此使用它会触发此错误。

不要直接从

params
访问它,而是创建一个仅返回此文件中所需的允许参数的方法,如下所示:

private dev locale_params
  params.permit(:locale)
end

然后更改您的查找代码以引用该代码:

private def extract_locale
  parsed_locale = locale_params[:locale]
  I18n.available_locales.map(&:to_s).include?(parsed_locale) ? parsed_locale : nil
end
© www.soinside.com 2019 - 2024. All rights reserved.