I'm trying to write a regex to match a show path of a nested resource. The path goes like this: /users/:user_id/products/:id - I have been unable to write a regex to check if a url is for this path. I don't want anything after the products/:id to pass the match. Right now I have the following:
/users/([^&]*)/products/
This works in that it'll match the url path I'm looking for, but I cant figure out how to end it after the /products/:id so it won't match actions such as /edit. So users/1-my-name-is-bob/products/1-awesome-product should match, but users/1-my-name-is-bob/products/1-awesome-product/edit should not match the regex.
try to use this pattern:
\/users\/([^&\/]+)\/products\/[^\/]+\Z
\Z stand for end-of-the-string
you can remove slashes at the begining if not needed, or make it optional with a question mark
Related
The wildcards seem to only apply to non-string literals and text.
I want to match the text on a partial match.
Example:
source: org.*.application-* AND "*.pdf"
The wildcard works with the source param but not instead of my literal.
How can I use a wildcard inside a string to get back all that match that contains a log with
"XXXXXXXXXXXX.pdf"
Okay - it turns out you remove the quotes - Anytime you add quotes the whole word must match
so this turns out to be the equivalent of what I was asking for:
source: org.*.application-* AND *.pdf
I know that's it's technically possible to create a path that allows any character in it by using constraints. However, I have found that it's impossible to create a route that has a dot at the base of the path, e.g.
http://localhost:5000/.well-known/acme-challenge/
This is the route I've been using:
get ':my_root/acme-challenge/:id', to: 'pages#letsencrypt', constraints: {my_root: /.+/}
Anyone have any ideas?
take a look at dynamic-segments in blue you will find
By default, dynamic segments don't accept dots - this is because the
dot is used as a separator for formatted routes. If you need to use a
dot within a dynamic segment, add a constraint that overrides this –
for example, id: /[^/]+/ allows anything except a slash.
you can try
get ':my_root/acme-challenge/:id', to: 'pages#letsencrypt', controller: /my_roo\/[^\/]+/
I hope that this helps
I want to match the following URLs to the same controller in my Rails app
/controller/folder1/folder2/
/controller/folderA/folderB/somefile
/controller/folderX/somefile
I currently can achieve the desired result if I limit the number of nested folders like so in my Routes.rb:
match '/controller(/:folder1)(/:folder2)(/:file)' => 'myspecial_controller#myaction'
Please note that the following does not work and an URL with multiple path components is not matched:
match '/controller/:full_path
I am wondering if there is some type of wildcard. So that I would ideally get an array of the path components that I can then assemble in the controller.
From documentation
match '/controller/*full_path'
I have the following Regexp to create a hash of values by separating a string at a semicolon:
Hash["photo:chase jarvis".scan(/(.*)\:(.*)/)]
// {'photo' => 'chase jarvis'}
But I also want to be able to have URL's in the string and recognize it so it maintains the URL part in the value side of the hash i.e:
Hash["photo:http://www.chasejarvis.com".scan(/(.*)\:(.*)/)]
// Results in {'photo:http' => '//www.chasejarvis.com'}
I want, of course:
Hash["photo:chase jarvis".scan(/ ... /)]
// {'photo' => 'http://www.chasejarvis.com'}
If you only want to match up to first colon you could change (.*)\:(.*) to ([^:]*)\:(.*).
Alternatively, you could make it a non-greedy match, but I prefer saying "not colon".
How do figure out a person's family name and first name?
Changing chasejarvis to chase and jarvis might not be possible unless you have a solution for that.
Do you already know everyone's name in your project? Nobody is having the initial of a middle name like charvisdjarvis (assuming the name is "Charvis D. Jarvis".)?
When one selects a tag on Stack Overflow, it is added to the end of the URL. Add a second tag and it is added to the end of the URL after the first Tag, with a + delimiter. For example:
http://stackoverflow.com/questions/tagged/ruby-on-rails+best-practices
How is this implemented? Is this a routing enhancement or some logic contained in the TagsController? Finally, how does one 'extract' these tags for filtering (assuming that they are not in the params[] array)?
Vojto's answer is correct, but note that you can also use Route Globbing on the server side to handle this cleanly. A route defined as /:controller/*tags will match /questions/ruby/rails/routing, and in the questions_controller, params[:tags] will be an array containing ['ruby','rails','routing']. See the Routing docs.
I think Rails doesn't mind if params contains symbols like +. That means, you can access all tags as one argument, create a route like: '/show/:tags'
Then you can access params[:tags], which will be string like 'ruby+rails'. You can simply do 'ruby+rails'.split('+') to turn it into an array.
By that you can easily append new tag to this array, and turn it back into string with my_array_with_tags.join('+').