how do I ignore a term in a route? - ruby-on-rails

I'm creating a route that should have an optional/ignored last term.
Like so:
/product/12345/Dark-Knight-Rises # last term is just there for a nice URL
I was thinking, from reading the docs, that I'd just be able to wildcard the last term:
match 'product/:uid/*full_name' => 'product#view', :via => [:get]
That didn't work. I did get this to work:
match 'product/:uid/:full_name' => 'product#view', :via => [:get]
match 'product/:uid' => 'product#view', :via => [:get]
But, well, it seems like this should be doable in one line. Yes?

match 'product/:uid(/:full_name)' => 'product#view', :via => [:get] is what you are looking for
The parenthesis make the full_name an optional parameter which you can just ignore since all you want is a pretty URL.

Below single line should work
match 'product/:uid/:full_name' => 'product#view', :via => [:get]

Related

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.

Correct route file

Can someone help me to correct that?
match 'graphs/(:id(/:action))' => 'graphs#(:action)', :via => [:get, :post]
This is working as expected if parameter :action is specified but if it is missing I get :action not found. Is it possible to have a default controller if :action is missing or :id and :action together?
Thanks
you don't have to use 'controller#action'. You can also only specify the controller there:
match 'graphs/(:id(/:action))' => 'graphs', :via => [:get, :post]

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

get, match and resources in routes.rb

I am new to Rails. I found it very strange when I use the resources in routes.rb, after I redirect the page to controller/index, it render the controller/show .
I know GET controller/action is same as match "controller/action", :to => "controller/action"
I think the strange thing happens to me about the redirect, is similar to the GET and Match.
so I wonder what exactly the resources mean, can I use some simple match do the same thing?
resources is a shortcut for generating seven routes needed for a REST interface.
resources :widgets is equivalent to writing
get "widgets" => "widgets#index", :as => 'widgets'
get "widgets/:id" => "widgets#show", :as => 'widget'
get "widgets/new" => "widgets#new", :as => 'new_widget'
post "widgets" => "widgets#create", :as => 'widgets'
get "widgets/:id/edit" => "widgets#edit", :as => 'edit_widget'
patch "widgets/:id" => "widgets#update", :as => 'widget'
put "widgets/:id" => "widgets#update", :as => 'widget'
delete "widgets/:id" => "widgets#destroy", :as => 'widget'
it just saves you the trouble.
By the way, get is not exactly the same as match. get, post, put and delete are shortcuts for limiting the route to a single HTTP verb. The two route definitions below are equivalent.
match 'foo' => 'controller#action', :method => :get
get 'foo' => 'controller#action'

Resources