【问题标题】:Russian symbols in url and route constraintsurl 和路由约束中的俄罗斯符号
【发布时间】:2014-01-05 09:19:32
【问题描述】:

我的应用中有这样的网址:

local/alphabetical/service/Ю  
local/alphabetical/service/Б  
local/alphabetical/service/Ж 

我想在网址中只允许符号 Ю、Б、Ж

但是routes.rb

get "/alphabetical/:type/:letter" => "alpha#index",     
    :constraints => { :type => /good|service/, :letter => /[ЮБЖ]/ }, 
    :as => "alpha"

for http://local/alphabetical/service/Ю 给我一个错误:

Routing Error  
No route matches [GET] "/alphabetical/service/%D0%AE"  

如何在 routes.rb 文件中设置约束以只允许 Ю、Б、Ж 符号?

谢谢。

【问题讨论】:

    标签: ruby-on-rails url routes


    【解决方案1】:

    有一个类似的问题,为了一个例子而发布。

    resources :listings, path: "объявления" do
      get ":kind(/:category(/:subcategory(/:state(/:city))))",
          constraints: lambda { |req| %w(работа недвижимость услуги продажи).include? req.params[:kind] },
          to: "listings#search",
          as: "search",
          on: :collection
      end
    

    【讨论】:

      【解决方案2】:

      感谢@phoet 的回复,非常有用。
      有没有有趣的人...

      就我而言,解决方案是:

      产品型号:

      class Product < ActiveRecord::Base
          LETTERS = %w( А Б В Г Ґ Д Е Є Ж З І Ї Й К Л М Н О П Р С Т У Ф Х Ц Ч Ш Щ Ю Я )
      end
      

      routes.rb

      get "/alphabetical/:type/:letter" => "alpha#index",     
              :constraints => lambda { |req| req.params[:type] =~ /good|service/ and req.params[:letter] =~ /[#{Product::LETTERS.join}]/i }, 
              :as => "alpha"
      

      【讨论】:

        【解决方案3】:

        我认为您需要自己处理这些字符的转义。在此处查看此示例:Redirect when using I18n with Rails is encoding the forward slash as %2F

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-24
          • 2013-07-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多