Rails - Params with "Dot" (E.G. /Google.Com)

Rails — Params with dot (e.g. /google.com)

By default, dynamic segments don't accept dots - this is because the dot is used as a separator for formatted routes. However, you can add some regex requirements to the route parameters. Here, you want to allow the dots in the parameters.

match 'some_action/:id' => 'controller#action', :constraints  => { :id => /[0-z\.]+/ }

And in rails 2.3:

map.connect 'some_action/:id', :controller => 'controller', :action => 'action',  :requirements => { :id => /[0-z\.]+/ } 

Relevent rails guides section

Having a dot in the id of rails routes

Because of the :format convention, Rails will parse all parameters without any dots. You can have route parameters with dots if you want:

# You can change the regex to more restrictive patterns
map.connect 'users/:id', :controller => 'users', :action => 'show', :id => /.*/

But since both '*' and '+' regex wildcards are greedy, it will ignore the (.:format) param completely.

Now, if you absolutely need to have dots in the username, there is a pseudo-workaround that could help you:

map.connect 'users/:id:format', :controller => 'users', :action => 'show', :requirements => { :format => /\.[^.]+/, :id => /.*/ }
map.connect 'users/:id', :controller => 'users', :action => 'show'

The downside is that you have to include the dot in the :format regex, otherwise it would be caught by the username expression. Then you have to handle the dotted format (e.g.: .json) in your controller.

Rails REST routing: dots in the resource item ID

You could replace periods with another character:

def to_param
login.gsub(/\./,"-") # note: 'self' is not needed here
end

user = User.find_by_login("bart.simpson")
user_path(user) # => "/users/bart-simpson"

EDIT

You're right, this fails to deal with unique logins that map to the same value. Maybe a better way is to use segment constraints in the route:

  match 'users/(:id)' => 'users#show', 
:constraints => { :id => /[0-9A-Za-z\-\.]+/ }

This should allow "/users/bart-simpson" and /users/bart.simpson" to generate :id => "bart-simpson" and :id => "bart.simpson" respectively. You'd have to alter the regex to add all the acceptable characters for the URL.

Note that this is mentioned in the Rails Routing Guide, section 3.2:

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 which overrides this –
for example :id => /[^\/]+/ allows anything except a slash.

Parameter part after . is missing (.com)

I found how to fix this here : Rails — Params with "dot" (e.g. /google.com)

What happens is that rails don't get the .com extension of my email, results in an empty array.
This is my route for the MessageThread class :

resources :message_threads, constraints: { id: /[0-z\.]+/ }

Rails 5 Query from url address with params

As Ray said, if you add:

<%= link_to articles_path(author: "name_here") %>

that will generate the url http://localhost:3000/articles?author=name_here

You may find it easier to use a gem to easily implement a search for articles by author name but if you just want to do this manually in your ArticlesController:

def index
if params[:author]
@author = params[:author]
@articles = Article.all.where(author: @author)
else
@articles = Article.all
end
end

Dots in urls in Rails 3

This has already several questions for this issue, look around, I myself asked that.
Here is like I got it working (like here on stackoverflow, when you klick a Tag like ".net"):

get 'questions/tagged(/:tag)' => "clues#index", :tag => /.*/

Getting param from friendly url like http://localhost:3000/user/adem-balka with Rails 5

thank you for the answers. I learned from them /p>

and this solved my problem

  @user_id = User.friendly.find(params[:id])
@posts = Post.where(user_id: @user_id)


Related Topics



Leave a reply



Submit