Rails 3 routes with extended regex :constraints - bug? - regex

I have the following route in my Rails 3 app.
post 'games/:id/:task/:card_id' => 'games#perform', :as => :perform
...which allows, obviously, such requests as button_to("Foo", {card_id=>2, :action=>:perform, :task=>"foo"}), mapping to the URL /games/1/foo/2.
I'd like to restrict the set of tasks the route matches. The Rails API docs indicate that "Constraints can include the ‘ignorecase’ and ‘extended syntax’ regular expression modifiers". However, the following works as expected:
post 'games/:id/:task/:card_id' => 'games#perform', :as => :perform, :constraints => {:task => /(foo|bar)/}
But the following doesn't:
post 'games/:id/:task/:card_id' => 'games#perform', :as => :perform, :constraints => {:task => /(foo|
bar)/x}
In the latter case, the button_to link above produces the URL: /games/perform?card_id=2&task=foo.
Is this a bug, or am I doing something wrong?

Yeah, there's a bug in the RegexpWithNamedGroups processing for Ruby 1.8.x because it fakes the named regexp groups syntax of 1.9 in a way that is incompatible with regexp modifiers.
The problem here is that with 1.8 Rails uses a simple index of the paren group within the regexp to map to the name. When you add a modifier to a regexp, internally this creates an additional group which throws the index off by one.
Workaround right now is to use 1.9, or no modifier.

Related

regex to validate router parameter not working

I want to validate if an email is being send correctly in routes, however it isn't working when I put a regex condition (like explained here)
Basically, I added:
Route::group([
'namespace' => 'Auth',
'middleware' => 'api',
'prefix' => 'auth'
], function () {
Route::post('login', 'AuthController#login');
// ...
Route::get('checkemail/{email}', 'AuthController#check_email')->where('email', '#.*?\.');
});
even if I pass a valid email I get an 404 not found. what can I do to make the code works well?
It looks like you're missing .+ before # in your regex.
Edit: I forgot to mention, you can put the same thing after #, and the current regex seems like it's wrong as well. I won't go into it, but validating email should not be complicated for a lot of reasons.

Customize resource routing in Rails 4

think of the resource routing of a photo-class for example.
If I this to my routes.rb I will get following routes:
resources :photos
# GET '/photos/', :to => 'photos#index'
# GET '/photos/:photo_id/, :to => 'photos#show'
# and so on and so on
Now what I want is to replace the word /photos in all the routes with a simple /p so that I can get a short URL like /p/1 for the first photo. Is there a way to simply alter the resource-line or do I have to manually add each route?
This will make all your routes via :photos through p
resources :p, :controller => "photos"
To be more concise and avoid the issue with p_id, you could do it like this :
resources :photos, path: 'p'
This way, you benefits from the readibility on your end (it will generate helpers like edit_photo_path, you will access variables as photo_id in case of a nested route and such) and generate the named URLs you do want.

Rails: aliasing the index action in the routes file

I have the following route set up. I need to make the index action automatically use the pubmed_search route below.
resources :users do
resources :publications do
collection do
get :pubmed_search
post :pubmed_list
end
end
end
I tried
resources :users do
resources :publications do
collection do
get 'publications', :action => :pubmed_search
get :pubmed_search
post :pubmed_list
end
end
end
Without luck I could just do a redirect in the index method of the controller but i am sure there is a Rails way to do this and I want to learn.
EDIT:
This works
get "/users/:user_id/publications" => "publications#pubmed_search", :as => "user_publications"
But isn't there a better way, using the RESTful resources?
This works
get "/users/:user_id/publications" => "publications#pubmed_search", :as => "user_publications"

RegEx to match sub-string of a URL

I am in the need of a Regex to match single post permalink but not pagination page, category or certain exception pages (RSS feed page, contact page, etc):
valid => http://anysitexxxx.com/a-single-post/
invalid => http://anysitexxxx.com/
invalid => http://anysitexxxx.com/category/running
invalid => http://anysitexxxx.com/category/running/1
invalid => http://anysitexxxx.com/page/2
invalid => http://anysitexxxx.com/page/3
invalid => http://anysitexxxx.com/feed -> sample of certain execption
This is intended for Google Analytics URL goal matching purposes- can someone with this experience help me? Thanks
You will need to use negative lookahead with the exceptions you don't want
http:\/\/anysitexxxx\.com\/(?!category|page|feed).+
Demo

View overriding using Deface

I am trying to insert a text on show page of Spree's product page.
My app/overrides/show_new.html.erb as follows:
Deface::Override.new(:virtual_path => "spree/products/show",
:insert_after => "[data-hook='product_show']",
:text => "<p>This is overriding......</p>",
:name => "show_new",
:disabled => false)
But, the view is not overridden. Am I missing something?
You must restart your app for the overrides to register first.
Otherwise, this seems correct (assuming your code is in app/overrides/something.rb)
Watch out if you are not on latest version, some views HTML are broken and it breaks deface.
https://github.com/spree/spree/issues/1056 (I ran into this issue last week)