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
Warning: Constant ::Fixnum Is Deprecated When Generating New Model
Rails Route to Username Instead of Id
Ruby 1.9 Hash with a Dash in a Key
Typing 'Rails Console' Doesn't Start
Select Arrays Between Date Ranges with Ruby
Peer-To-Peer File Sharing with Web Sockets
Difference Between Has_One and Belongs_To in Rails
Using Factory_Girl in Rails with Associations That Have Unique Constraints. Getting Duplicate Errors
How to Build a Rubygems Mirror Server
Rails 3. How to Display Two Decimal Places in Edit Form
Creating a Model That Has a Tree Structure
Set Attribute Dynamically of Ruby Object
Why Can't Singleton Methods Be Defined on Symbols or Fixnums
Does Ruby Provide a Way to Do File.Read() with Specified Encoding
Is There Goto Statement in Ruby
Passing Multiple Error Classes to Ruby's Rescue Clause in a Dry Fashion