Rails 3: Replace /users/username/something for /username/something - ruby-on-rails

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

Related

Rails route to get a resource using query string

I would like to have a custom route querystring based, to access a specified resource. For example:
/opportunities/rent/san-miguel-de-tucuman?id=45045
That route should map to the action OpportunitiesController#show, with the resource id 45045.
Thanks in advance!!!
Updated
This are my current routes:
get 'oportunidades/alquiler/san-miguel-de-tucuman/:id', to: "opportunities#show"
get 'oportunidades/alquiler/san-miguel-de-tucuman', to: "opportunities#index"
So, if I navigate to the /oportunidades/alquiler/san-miguel-de-tucuman?id=123456 route, it go to the Opportunities#index action.
P/S: sorry, I forget to mention that I have a similar route for the index action.
Make your custom routes as:
resources: opportunities, except: :show
get '/opportunities/rent/san-miguel-de-tucuman/:id' => 'opportunities#show', :as => 'opportunities_show'
and pass your 'id' as opportunities_show_path(id)
EDIT
Change your routes as:
get 'oportunidades/alquiler/san-miguel-de-tucuman/:id' => "opportunities#show", :as => 'opportunities_show'
get 'oportunidades/alquiler/san-miguel-de-tucuman' => "opportunities#index", :as => "opportunities_index"
Now when you want to access your show page just use opportunities_show_path(:id =>123456 )
And for index page use opportunities_index_path
Use this
match '/opportunities/rent/san-miguel-de-tucuman/:id', :to => 'opportunities#show', :via => :get
and pass a object to the path so created. Eg:-
something_path(#object), here #object is object that with id which will be passed in routes
Option 1
Query string parameter
// /opportunities/rent/san-miguel-de-tucuman?id=45045
match '/opportunities/rent/san-miguel-de-tucuman', :to => 'opportunities#show', :as => "show_opportunity", :via => :get
Option 2
Add id like new parameter. More friendly.
// /opportunities/rent/san-miguel-de-tucuman/45045
match '/opportunities/rent/san-miguel-de-tucuman/:id', :to => 'opportunities#show', :as => "show_opportunity", :via => :get
In both cases, you need to call the route like this:
show_opportunity_url(:id => 45045)
In your controller you will get the id in params[:id]

How could I rewrite this set of routes more cleanly?

I feel like I should know this and I'm certain this could be done more cleanly, but I'm not quite sure the best way to go about it.
How could a set of routes like this be written in a more DRY way?
# Artists
match "/:id/remixes", :to => "artists#remixes", :as => "artist_remixes"
match "/:id/originals", :to => "artists#originals", :as => "artist_originals"
match "/:id/popular", :to => "artists#popular", :as => "artist_popular"
match "/:id/mashups", :to => "artists#mashups", :as => "artist_mashups"
match "/:id/covers", :to => "artists#covers", :as => "artist_covers"
match "/:id/productions", :to => "artists#productions", :as => "artist_productions"
match "/:id/features", :to => "artists#features", :as => "artist_features"
That should do it:
resources :artists, path: '/' do
member do
get 'remixes'
get 'originals'
get 'popular'
get 'mashups'
get 'covers'
get 'features'
end
end
I would look to try and do 1 route and pass list_type as a parameter.
Something like
resources: artists do
resources list_types
end
I would try and avoid having seperate actions for a bunch of methods that probably do similar things.
Ah, should have just thought this through (hungover today):
[:remixes_of, :remixes_by, :originals, :popular, :mashups, :covers, :productions, :features].each do |role|
match ":id/#{role}", to: "artists\##{role}", as: "artist_#{role}"
end

Rails: simple routing question about :as => :something

Using Rails 3.1 (not sure if this is applicable to 3.0, etc.)
In routes.rb what's the difference between:
match "team" => "users#index"
and
match "team" => "users#index", :as => :team
I ask because docs say:
3.6 Naming Routes
You can specify a name for any route using the :as option.
match 'exit' => 'sessions#destroy', :as => :logout
This will create logout_path and logout_url as named helpers in your application. Calling logout_path will return /exit
But, in first example above I have access to team_path & team_url in my views?!? So what's the :as => :team do exactly? I must be overlooking something as I've seen example code written like:
match "logout" => "sessions#destroy", :as => :logout
match "login" => "sessions#new", :as => :login
match "signup" => "users#new", :as => :signup
though from my limited testing the :as => :something seems redundant?!?
It seems redundant but it's not... when the name of your route differ from the name you want to give.
The ActionDispatcher does a lot of things by default. You should try to trigger rake routes in your console to test this behavior.
Another example is the shortcut:
match "account/profile"
# same as
match "account/profile", :to => "account#profile"
which will create the named route: account_profile

How do I 301 redirect these route changes?

I have a couple routes like this:
match ':category/:brand/:permalink' => 'products#show', :as => :public_product
match 'stoves' => 'home#stoves', :as => :stoves
I changed them to this:
match ':category/:brand/:permalink' => 'products#show', :as => :public_product
match 'wood_stoves' => 'home#wood_stoves', :as => :stoves
I changed the category record titled stoves to wood_stoves.
Can I add a route redirect that allows for wildcards that would change anything like domain.com/stoves or domain.com/stoves/morso/8140-contemporary to domain.com/wood_stoves or domain.com/wood_stoves/morso/8140-contemporary, respectively? Or should I put this in my apache virtualhost config block?
Let your route.rb to be like this:
match ':category/:brand/:permalink' => 'products#show', :as => :public_product
match 'stoves' => 'home#stoves', :as => :old_stoves # anything else except stoves
match 'wood_stoves' => 'home#wood_stoves', :as => :stoves
In your Home Controller:
def stove
redirect_to stoves_path(# Use parameters here when a user hits a link like domain.com/stoves or domain.com/stoves/morso/8140-contemporary)
end
def wood_stoves
# your code here..
end
match ':category/:brand/:permalink' => 'products#show', :as => :public_product
match 'stoves' => 'home#stoves', :as => :old_stoves #
match 'wood_stoves' => 'home#stoves', :as => :stoves
This is enough i think.
If you are so particular about to change the name of the action to wood_stoves then i think you need to do Surya's solution

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