Allowing Underscore in Username Routes in Rails app - ruby-on-rails

Just wondering how I can edit my routes file to allow routing of usernames with an underscore character "_" in them. Here's the relevant code:
controller :users do
match ':id' => :show, :via => :get, :constraints => { :id => /[A-Za-z0-9\-\+]+/ }
end

Update your regular expression to add an _ as follows:
/[A-Za-z0-9\-\_\+]+/
The completed match will look like this:
match ':id' => :show, :via => :get, :constraints => { :id => /[A-Za-z0-9\-\_\+]+/ }

The construction A-Za-z0-9\_ is the same as \w. So we can use shorter version:
/[\w\-\+]+/

I think this regex pattern doesn't ignore this test string:
XXXX XXXXX
It has space between string.
If you update this regex pattern it will ignore this that test string:
controller :users do
match ':id' => :show, :via => :get, :constraints => { :id => /^[\w\-\+]+$/ }
end

Related

RAILS: Rendering views from the same controller based on the route

I have two routes (defined in my config/routes.rb)
match 'compare' => 'front_office#search_by_id', :via => :get, :as => :front_office_compare
match 'full_report' => 'front_office#search_by_id', :via => :get, :as => :front_office_full_report
I would like to know how I can tell my controller to render the view based on my route, without adding a parameter on my URL.
Based on this Question&Answer I Managed to get the result I want with
match 'compare' => 'front_office#search_by_id', :via => :get, :as => :front_office_compare, :defaults => { :render => 'compare' }
match 'full_report' => 'front_office#search_by_id', :via => :get, :as => :front_office_full_report, :defaults => { :render => 'full_report' }
And in my controller I defined my action as:
def search_by_id
render :action => (params[:render] || "full_report")
end
But is this a Good Practice or is there a better way to do it?
Instead of creating different routes for each category you are making for simplifying you can write it like:
# config/routes.rb
get ":category", to: "front_office#search_by_id", as: 'front_office', constraints: {category: /(compare|full_report)/}
the above routes looks for /compare and /full_report and this will call search_by_id action in front_office controller.
then inside the controller do as follows:
def search_by_id
render params[:category]
end
params[:category] will hold the slug values which we passed through the URL

Rails routing, matching short urls?

I'm having an issue where I've created a route I'm using to match short token like urls, like this:
myapp.com/a2c3b
I'm doing that by using a route like this:
match '/:id' => 'items#show', :as => "show_item", :via => :get, :constraints => { :id => /[a-z0-9]{5}/ }
But the issue is that now my other routes like /admin don't work because that also has 5 characters, how can I work around this, and have both kinds of routes work?
Put all of your routes that would match before this route in the file... that is...
match '/admin'....
match '/login'....
match '/:id' => 'items#show', :as => "show_item", :via => :get, :constraints => { :id => /[a-z0-9]{5}/ }

Subdomain for nested resources in Rails

My rails app is set to use subdomains as described in this RailsCast:
http://railscasts.com/episodes/221-subdomains-in-rails-3
However, right now, paths render like this:
http://organization.domain.com/organizations/1/edit
I have the controllers set up to choose the organization based on the subdomain already, so I'm wondering if there's a way to strip out the /organizations/:id portion of the paths, such that:
link_to edit_organization(#organization)
goes to http://organization.domain/edit, instead of http://organization.domain/organizations/:id/edit
Since there's going to be many nested resources within organizations (people, donations, etc), it's important that URL's don't end up incredibly long, and that the path generation method remains pretty straightforward.
Is there a way to do this?
You could use a route like:
resource :organization, :path => ""
The will cut down your url to 'http://organization.domain/:id/edit`.
Getting rid of the :id is tricky, and I dont think it can be done directly. What I would do is something like:
resource :organization, :path => "", :only => [] do
match "index", :via => :get
match "new", :via => :get
match "show", :via => :get, :constraints => {:subdomain => /[a-zA-Z]+/}
match "edit", :via => :get, :constraints => {:subdomain => /[a-zA-Z]+/}
match "update", :via => :put, :constraints => {:subdomain => /[a-zA-Z]+/}
match "create", :via => :post
end
Not very DRY, but I think it should work.

With the route generated by the Vanity gem, what is the Rails route helper I can use?

This is the route the Vanity gem generates:
controller :vanities do
match ':vname' => :show, :via => :get, :constraints => {:vname => /[A-Za-z0-9\-\+]+/}
end
This is the rake routes:
GET /:vname(.:format) {:vname=>/[A-Za-z0-9\-\+]+/, :controller=>"vanities", :action=>"show"}
How do I use the Rails link helper to link directly to URL mydomain.com/vname?
From the top of my head (sorry, I don't really have the time to test it right now):
controller :vanities do
match ':vname' => :show, :via => :get, :constraints => {:vname => /[A-Za-z0-9\-\+]+/}, :as => :vanity
end
which you would use like this:
vanity_path(:vname => "marcamillion")

Rails 3: Replace /users/username/something for /username/something

I already have a route to match /username for the users show page. However, when I add another action, for example: followers and following as below:
resources :users, :only => [:show] do
get :following, :followers
end
I get the URL /users/username/following instead of /username/following.
How can I make all the /users/username URL's be matched as /username/etc.. ?
Here's my /username route:
match '/:id' => 'users#show', :constraints => { :id => /[a-zA-Z0-9\-_]*/ }, :as => "user_profile"
thank you.
Edit:
I have fixed this by adding
match '/:id/following' => 'users#show/following', :as => "user_following"
match '/:id/followed' => 'users#show/following', :as => "user_followers"
But I'm not sure if that's the best solution. What I'd really want is a default route that would match all /:id/:action to the appropriate action omitting the /users.
I have fixed this by adding
match '/:id/following' => 'users#show/following', :as => "user_following"
match '/:id/followed' => 'users#show/following', :as => "user_followers"
But I'm not sure if that's the best solution. What I'd really want is a default route that would match all /:id/:action to the appropriate action omitting the /users.
Near the bottom
match ':id/:action', :controller => :users

Resources