Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.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 设计和解决I18n-重置密码路由问题_Ruby On Rails_Ruby On Rails 3_Internationalization_Routing_Devise - Fatal编程技术网

Ruby on rails 设计和解决I18n-重置密码路由问题

Ruby on rails 设计和解决I18n-重置密码路由问题,ruby-on-rails,ruby-on-rails-3,internationalization,routing,devise,Ruby On Rails,Ruby On Rails 3,Internationalization,Routing,Devise,我将I18n添加到使用Desive的RoR应用程序中,如果我尝试重置密码,现在会出现错误。错误是: Routing Error No route matches {:action=>"edit", :controller=>"devise/passwords", :reset_password_token=>"uMopWesaxczNn2cdePUQ"} 如何正确设置我的Desive路由以考虑I18n routes.rb scope ":locale", locale:

我将I18n添加到使用Desive的RoR应用程序中,如果我尝试重置密码,现在会出现错误。错误是:

Routing Error
No route matches {:action=>"edit", :controller=>"devise/passwords", :reset_password_token=>"uMopWesaxczNn2cdePUQ"} 
如何正确设置我的Desive路由以考虑I18n

routes.rb

 scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do   
  devise_for :users, path_names: {sign_in: "login", sign_out: "logout"},
                   controllers: {omniauth_callbacks: "omniauth_callbacks"}
  root to: 'static_pages#home'
 end

   match '*path', to: redirect {|params| "/#{I18n.default_locale}/#{CGI::unescape(params[:path])}" }, constraints: lambda { |req| !req.path.starts_with? "/#{I18n.default_locale}/" }
   match '', to: redirect("/#{I18n.default_locale}")
应用程序\u控制器.rb

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

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

describe "Routes" do

  describe "locale scoped paths" do
    I18n.available_locales.each do |locale|

      describe "routing" do
        it "should route /:locale to the root path" do
          get("/#{locale.to_s}").
            should route_to("static_pages#home", locale: locale.to_s)
        end
      end

      describe "redirecting", type: :request do

        subject { response }

        context "fake paths" do
          let(:fake_path) { "fake_path" }

          before { get "/#{locale.to_s}/#{fake_path}" }
          it { should redirect_to(locale_root_path(locale)) }
        end
      end
    end
  end

  describe "non-locale scoped paths" do

    describe "redirecting", type: :request do

      subject { response }

      context "no path given" do
        before { get "/" }
        it { should redirect_to(locale_root_path(I18n.default_locale)) }
      end

      context "a valid action" do
        let(:action) { "about" }
        let!(:default_locale_action_path) { about_path(I18n.default_locale) }

        context "with a valid but unsupported locale" do
          let(:unsupported_locale) { "fr" }

          before { get "/#{unsupported_locale}/#{action}" }
          it { should redirect_to(default_locale_action_path) }
        end

        context "with invalid information for the locale" do
          let(:invalid_locale) { "invalid" }

          before { get "/#{invalid_locale}/#{action}" }
          it { should redirect_to(default_locale_action_path) }
        end

        context "with no locale information" do
          before { get "/#{action}" }
          it { should redirect_to(default_locale_action_path) }
        end
      end

      context "invalid information" do
        let(:invalid_info) { "invalid" }

        before { get "/#{invalid_info}" }
        it { should redirect_to("/#{I18n.default_locale}/#{invalid_info}") }
        # This will then get caught by the "redirecting fake paths" condition
        # and hence be redirected to locale_root_path with I18n.default_locale
      end
    end
  end
end

我对设置Desive不太了解,但我确实花了一些时间研究了Rails路由的国际化,所以希望这个答案对您有用,如果不是作为您问题的答案,那么可以作为接近答案的参考(内容主要是我写的评论的翻版,也是Rails i18n路由信息的良好来源):

您的应用程序\u controller.rb在我看来很好,但也许可以尝试更改您的路由.rb,使其看起来像这样:

配置/routes.rb(示例)

由于有两个
根路径
,因此我重命名了
:locale
范围内的路径,以便应用程序和测试中不会发生冲突。我使用RSpec测试了路由,如下所示:

spec/routing/routing\u spec.rb

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

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

describe "Routes" do

  describe "locale scoped paths" do
    I18n.available_locales.each do |locale|

      describe "routing" do
        it "should route /:locale to the root path" do
          get("/#{locale.to_s}").
            should route_to("static_pages#home", locale: locale.to_s)
        end
      end

      describe "redirecting", type: :request do

        subject { response }

        context "fake paths" do
          let(:fake_path) { "fake_path" }

          before { get "/#{locale.to_s}/#{fake_path}" }
          it { should redirect_to(locale_root_path(locale)) }
        end
      end
    end
  end

  describe "non-locale scoped paths" do

    describe "redirecting", type: :request do

      subject { response }

      context "no path given" do
        before { get "/" }
        it { should redirect_to(locale_root_path(I18n.default_locale)) }
      end

      context "a valid action" do
        let(:action) { "about" }
        let!(:default_locale_action_path) { about_path(I18n.default_locale) }

        context "with a valid but unsupported locale" do
          let(:unsupported_locale) { "fr" }

          before { get "/#{unsupported_locale}/#{action}" }
          it { should redirect_to(default_locale_action_path) }
        end

        context "with invalid information for the locale" do
          let(:invalid_locale) { "invalid" }

          before { get "/#{invalid_locale}/#{action}" }
          it { should redirect_to(default_locale_action_path) }
        end

        context "with no locale information" do
          before { get "/#{action}" }
          it { should redirect_to(default_locale_action_path) }
        end
      end

      context "invalid information" do
        let(:invalid_info) { "invalid" }

        before { get "/#{invalid_info}" }
        it { should redirect_to("/#{I18n.default_locale}/#{invalid_info}") }
        # This will then get caught by the "redirecting fake paths" condition
        # and hence be redirected to locale_root_path with I18n.default_locale
      end
    end
  end
end
我确实为这种情况创建了一个完整的应用程序(Desive+国际化)。自从我创建这个应用程序以来已经有一段时间了,它可能有点缺陷/不完整,但关键是使用带括号的可选范围

代码的问题是,
design\u for:users
在没有
:locale
变量集的情况下没有定义(这是我从错误中猜测的,路由中的重定向代码可能不起作用-你真的不需要,我没有测试过,但我认为这不是一个好的做法)。此外,这也是它试图将令牌值指定为
:locale
变量的原因

相反,您需要使用括号。这样,
:locale
将是可选的,并且当未设置
:locale
时,您的路线定义将保持有效

scope "(:locale)", :locale => /en|tr/ do
  devise_for :users
  root :to => "main#index"
end
我希望这能有所帮助。

我也遇到了同样的问题(密码重置时出现路由错误),但我找到了以下解决方案: 路由错误不是由控制器或任何其他设计内部进程引起的,而是由视图引起的。在
views/designe/mailer/reset\u password\u instructions.html.erb
行中:

<%= link_to 'Change my password', edit_password_url(@resource, :reset_password_token => @resource.reset_password_token) %>
@resource.reset\u password\u token)%>
需要替换为:

<%= link_to 'Change my password', edit_password_url(@resource, :reset_password_token => @resource.reset_password_token, :locale => I18n.locale) %>`
@resource.reset\u password\u令牌,:locale=>I18n.locale)%>`

我不知道为什么默认情况下不添加locale参数。

谢谢您的回答;但是,即使我按照您的建议更改了routes.rb,我仍然会收到相同的错误。我认为我的问题更多地与设计和如何正确覆盖密码重置路由有关。不用担心。我希望有人设计专业知识给你的信息,你需要的!你好,保罗。在测试了您的示例之后。我认为我的问题不一定与设计有关。我尝试使用routes.rb示例和常规密码重置控制器(如Railscasts第274集中使用的)。在这种情况下,我仍然会遇到同样的错误,它试图传递reset_password_令牌作为locale参数
路由错误No Routing error matches{:action=>“edit”,:controller=>“password_resets”,:locale=>“1qbkCGXrNx3tLkqCGvMrHw”}
您使用I18n进行了密码重置以正确工作吗?如果是这样的话,你介意展示一些示例代码吗?
:locale=>“1qbkCGXrNx3tLkqCGvMrHw”
字符串对我来说很奇怪…是designe,还是你的代码,将你的应用程序的区域设置更改为某个加密值?我还没有完成密码重置的实现,但请随时检查并从i18n in获取您想要的内容,因为它可能会对您有所帮助。具体来说,文件config/initializers/i18n.rb、app/controllers/application_controller.rb、app/views/layouts/application.html.haml app/views/layouts/_locale_selector.html.haml该值是密码重置令牌。由于某些原因,当表单尝试发送时,密码重置令牌正在替换区域设置。我会查你的档案,谢谢。哇,谢谢!令人惊讶的是,一组缺少的括号给我带来了如此多的麻烦。我很高兴它确实有帮助!rails i18n documentation guide(rails i18n文档指南)中记录了此功能,但有点难以注意:)这肯定有助于解决问题,但这不会导致另一个问题,即重复的URL吗?如果使用括号,则不允许使用所有区域设置。因此,相同的内容可以通过多个Url访问……考虑到Georg在评论中提出的问题,我认为Krzystar的回答是一个更好的解决方案。在决定不将locale作为可选参数时,我考虑了同样的问题。对于任何正在寻找解决方案的人,请看一下Krzystar的解决方案。