Russian symbols in url and route constraints - ruby-on-rails

There are such urls in my app:
local/alphabetical/service/Ю
local/alphabetical/service/Б
local/alphabetical/service/Ж
I would like to allow only symbols Ю, Б, Ж in the url
But routes.rb:
get "/alphabetical/:type/:letter" => "alpha#index",
:constraints => { :type => /good|service/, :letter => /[ЮБЖ]/ },
:as => "alpha"
for http://local/alphabetical/service/Ю Gives me an error:
Routing Error
No route matches [GET] "/alphabetical/service/%D0%AE"
How to setup a constraint in routes.rb file to allow only Ю, Б, Ж symbols?
Thanks.

Thanks #phoet for reply, its very useful.
Will someone interesting...
For my case the solution is:
Product model:
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"

I think that you need to handle the unescaping of those characters yourself. Have a look at this example here: Redirect when using I18n with Rails is encoding the forward slash as %2F

had a similar issue, post for the sake of an example.
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

Related

No Route Matches [GET] with # or ? parameter error

I have this route:
# :chwid => Camera Hardware ID - :mid => Machine ID - :fs => FormatString
get '/videocams/video/:chwid/:mid/:fs' => "videocams#get_videocams_id", :constraints => { :chwid => /[^\/]+/, :mid => /[^\/]+/, :fs => /[^\/]+/ }
But when I call the route, my first parameter :chwid contains the character # and ?:
(#device:pnp:\?\root#image#0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global)
and an error is called:
Started GET "/videocams/video/#device:pnp:%5C%5C?%5Croot"
ActionController::RoutingError (No route matches [GET] "videocams/video/#device:pnp:%5C%5C":
What might be triggering this error and how can I fix it?
URL-escape characters that have meaning in URLs lie # and ?.

How to put an email address in url on Rails

I want to invite people who passes their email inside an url like this:
localhost:3000/invite_me/email#gmail.com
I tried this match but it isn't working.
match "/invite_me/:email" => "application#invite_me",
:constraints => { :email => '/.+#.+\..*/' }
I'm getting the following error:
No route matches [GET] "/invite_me/waldyr.ar#gmail.com"
rake routes output:
root / application#index
/invite_me/:email(.:format) application#invite_me {:email=>"/.+#.+\\..*/"}
Your constraint needs to be an actual regular expression and not a string
match "/invite_me/:email" => "application#invite_me",
:constraints => { :email => '/.+#.+\..*/' }
Should be
match "/invite_me/:email" => "application#invite_me",
:constraints => { :email => /.+#.+\..*/ }

Rails Routing help

I have the following in my routes.rb:
match "/profile/permissions" => 'profiles#edit_permissions', :as => 'edit_permissions', :via => :get
match "/profile/permissions" => 'profiles#update_permissions', :as => 'update_permissions', :via => :post
Getting /profile/permissions works find, yet, when I post to /profile/permissions, I get:
Started POST "/profile/permissions" for 127.0.0.1 at 2011-03-29 12:12:09 -0400
ActionController::RoutingError (No route matches "/profile/permissions"):
Any ideas?
Can you put your routes.rb in a gist? Sometimes there's a conflict, and extra eyes are needed to find it. Also, for the sake of fidgeting, you might try out the new shorthand:
get "/profile/permissions" => 'profiles#edit_permissions', :as => 'edit_permissions'
post "/profile/permissions" => 'profiles#update_permissions', :as => 'update_permissions'
Unto themselves, both your routing text and this example successfully trigger their actions on the specified controller.
Start a test app, with only a routing file, run its server, and try to post to those URLs. If you want to use the Rails console, check out this.
> require "uri"
> require "net/http"
> url = app.update_permissions_url
> url = "http://localhost/profile/permissions" (if your domain is bollocks)
> Net::HTTP.post_form(URI.parse(url),{})
Easy to minimalize your environment for testing, now.

How do you make Rails route everything under a path to a Rack app?

I'm trying to capture all requests to /dav and all paths nested under that to a Rack handler:
match "/dav" => RackDAV::Handler.new(:root => 'davdocs')
match "/dav/*whatever" => RackDAV::Handler.new(:root => 'davdocs')
Do I really have to make two routes for this, or is there a way to express this as one route (one line)?
I think it should be enough to use
match "/dav(/*whatever)" => RackDAV::Handler.new(:root => 'davdocs')
Optional parameters are very briefly described in the Rails Routing guide under "Bound parameters"
match '/dav(/*dav_section)', :to => Proc.new { |env| [200, {"Content-Type" => 'text/plain'},["Here we are in Dav"]]}

rails functional testing issue

When I do
get :inside, :format => :xml, :lat2 => "41", :lng2 => "-73.9", :lat1 => "40", :lng1 => "-74", :category => "girl", :order => "date"
with my routes.rb includes:
get 'images/inside/:lat1/:lng1/:lat2/:lng2/:order/:category', :to => "images#inside"
I get
ActionController::RoutingError: No route matches {:lng1=>"-74", :category=>"girl", :lat2=>"41", :format=>:xml, :lng2=>"-73.9", :order=>"date", :lat1=>"40", :action=>"inside", :controller=>"images"}
But when I do
get :inside, :format => :xml, :lat2 => "41", :lng2 => "-73", :lat1 => "40", :lng1 => "-74", :category => "girl", :order => "date"
it works!
The only difference is the decimal value of lng2.
Note that if routes.rb has no params, it works, but I need those
As far as I can tell, it's because your value has a period. By default, Rails (at least Rails3) routes cannot contain periods. To work around this, see http://avdi.org/devblog/2010/06/18/rails-3-resource-routes-with-dots-or-how-to-make-a-ruby-developer-go-a-little-bit-insane/
I bet the decimal point is making Rails think that there's a format being specified (e.g. .xml or .js).
You might be able to get around it using a regex, something like what's described here: http://zargony.com/2009/05/05/routing-parameters-with-a-dot

Resources