No Route Matches [GET] with # or ? parameter error - ruby-on-rails

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 ?.

Related

Running minitest controller get ActionController::UrlGenerationError: No route matches

I know, that with this topic more questions asket, but i don't found what i need.
Currently i'm updating rails app from 3.2.13 to 4.2.0 and after upgrading rails naturally fails tests. These tests are passed in 3.2.13
So, i have this route:
get '/catalogs/:article_id/get_applicability_by_brand/:brand_id', :to => 'catalogs#get_applicability_by_brand', constrains: { format: 'js' }, as: :catalog_get_applicability_by_brand
Result of rake routes like this:
catalog_get_applicability_by_brand GET /catalogs/:article_id/get_applicability_by_brand/:brand_id(.:format) catalogs#get_applicability_by_brand {:constrains=>{:format=>"js"}}
Controller action, it only render js.erb template:
def get_applicability_by_brand
#applicability = CatalogAccess::TecDoc.get_applicability_by_brand(params[:article_id], params[:brand_id])
end
Minitest controller test:
def test_get_applicability_by_brand_action
expected_applicability = [
{ 'model_name' => 'Model 1',
'name' => 'fake name',
'year_of_construct_from' => '2000',
'year_of_construct_to' => '2010',
'construction_type' => 'fake type' },
{ 'model_name' => 'Model 1',
'name' => 'fake name 2',
'year_of_construct_from' => '1991',
'year_of_construct_to' => '2005',
'construction_type' => 'fake type' }
]
CatalogAccess::TecDoc.expects(:get_applicability_by_brand).with('12', '23').returns expected_applicability
xhr :get, :get_applicability_by_brand, :article_id => '12', :brand_id => '23', :format => "js"
assert_response 200
assert_template 'get_applicability_by_brand'
assert_template :partial => '_tecdoc2_applicability'
end
Test error message is:
ActionController::UrlGenerationError: ActionController::UrlGenerationError: No route matches {:action=>"get_applicability_by_brand", :article_id=>"12", :brand_id=>"23", :controller=>"catalogs", :format=>"js"}
I found that if append to my test option 'use_route', it will be pass, but get warning that seems not good solution
xhr :get, :get_applicability_by_brand, :article_id => '12', :brand_id => '23', :format => "js", :use_route => 'catalogs'
Warning message:
DEPRECATION WARNING: You are trying to generate the URL for a named route called "catalogs" but no such route was found. In the future, this will result in an `ActionController::UrlGenerationError` exception. (called from test_get_applicability_by_brand_action at /home/sdilshod/webapp/ps_base/apps/www/test/controllers/catalogs_controller_test.rb:627)
DEPRECATION WARNING: Passing the `use_route` option in functional tests are deprecated. Support for this option in the `process` method (and the related `get`, `head`, `post`, `patch`, `put` and `delete` helpers) will be removed in the next version without replacement. Functional tests are essentially unit tests for controllers and they should not require knowledge to how the application's routes are configured. Instead, you should explicitly pass the appropiate params to the `process` method. Previously the engines guide also contained an incorrect example that recommended using this option to test an engine's controllers within the dummy application. That recommendation was incorrect and has since been corrected. Instead, you should override the `#routes` variable in the test case with `Foo::Engine.routes`. See the updated engines guide for details. (called from test_get_applicability_by_brand_action at /home/sdilshod/webapp/ps_base/apps/www/test/controllers/catalogs_controller_test.rb:627)
DEPRECATION WARNING: You are trying to generate the URL for a named route called "catalogs" but no such route was found. In the future, this will result in an `ActionController::UrlGenerationError` exception. (called from test_get_applicability_by_brand_action at /home/sdilshod/webapp/ps_base/apps/www/test/controllers/catalogs_controller_test.rb:627)
Advise me please correct solution.
I'll hope your help, thanks!

Rails Engine not routing in webpage

I am trying to mount a rails engine piggybak_paypal.
Inside my application's config/routes.rb file I add
mount PiggybakPaypal::Engine => '/paypal', :as => 'piggybak_paypal'
and the engines route is like this
PiggybakPaypal::Engine.routes.draw do
get "/express" => "paypal#express", :as => :paypal_express
get "/process" => "paypal#process_express", :as => :paypal_process
end
When I try rake routes the route of the engine is showing correctly
Routes for PiggybakPaypal::Engine:
paypal_express GET /express(.:format) piggybak_paypal/paypal#express
paypal_process GET /process(.:format) piggybak_paypal/paypal#process_express
but when I open my website and use /express the server cannot find the route
ActionController::RoutingError (No route matches [GET] "/express"):
I've look around but I can't find a solution to it.
okay I just figure out why the routing is not working and I can't believe I spend hours on this and didn't found out why.
All I did is change the route from
mount PiggybakPaypal::Engine => '/paypal', :as => 'piggybak_paypal'
to
mount PiggybakPaypal::Engine => '/', :as => 'piggybak_paypal'
then /express works.

Russian symbols in url and route constraints

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

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.

Resources