我的应用中有这样的网址:
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文件中设置约束以仅允许Ю,Б,Ж符号?
感谢。
答案 0 :(得分:2)
我认为你需要自己处理这些角色的问题。请在此处查看此示例:Redirect when using I18n with Rails is encoding the forward slash as %2F
答案 1 :(得分: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"
答案 2 :(得分:2)
有一个类似的问题,为了一个例子发布。
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