Remove controller from friendly URL - ruby-on-rails

I have written freindly URLs for the show action of the School Resource but now have
before i had ;
http://webaddress/schools/2
and now i have;
http://webaddress/schools/school_name
However, i want
http://webaddress/school_name
My config routes look like this for the resource;
resources :schools do
collection do
match 'search' => 'schools#search', via: [:get, :post], as: :search
end
end
How can i achieve that? thank you

Add this at the last your routes file:
match ':id' => 'schools#show', via: [:get]

A more conventional way would be to use the path: option in your route resources, like this:
#config/routes.rb
...
resources :schools, path: "", only: :show #-> has to go at end of file!
This will give you the ability to add different methods to this, as well as keeping with Rails conventions :)

Related

Rails routes change URL with ressources

I have the following in my routes:
resources :collection_pages, :only => [:show, :index]
Right now its going to wwww.mysite.com/collection_pages for index
and collection_pages/:id for show
I need it to go to /collections instead for index
and /collection/:id for show
I tried path: :collections , but its not behaving as I'd like.
What's the best way to achieve this using ressources?
Along with the SO answer #Tamer.Shlash notes, you could use custom matchers in your routes file:
match '/collection', to: 'collection_pages#index', as: 'collection_index'
match '/collection/:id', to: 'collection_pages#show', as: 'collection_show'
Adding the as: 'xxxx' to the match allows you to use friendly path and url helpers in your views:
redirect_to collection_index_path

Aliasing routes in rails 4

If I have the following routes
resources :pages do
resources :sections
end
I get routes that look like this:
/pages = #index
/pages/:id = #show
/pages/:id/edit = #edit
...etc
How can I go about making it so that the url for the #show action of the pages controller looks like '/:id', without the '/pages/' prefix? should I exclude #show from resources :page & create a get route + alias for it separately? or is there a way to do it from inside the resources :page block? Thanks in advanced.
EDIT:
Changed it to:
resources :pages, except: [:show] do
resources :sections
end
get '/:id', to: 'pages#show'
& rerouting non-existing :ids' to 404 for now, let me know if there's a better solution. Thanks.
get '/:id', to: 'pages#show', as: 'page'
Make sure this is at the bottom of your routes.rb file, otherwise it is going to hijack requests to other routes.
This also gives you page_url and page_path helper methods. But to use them you must exclude show action from previous routes.
resources :pages, except: [:show]

Rails nesting scope block in resources

I'm trying to create a path like product/:id/monthly/revenue/ and product/:id/monthly/items_sold and the equivalent named routes product_monthly_revenue and product_monthly_items_sold, and these routes would simply show the charts. I tried
resources :products do
scope 'monthly' do
match 'revenue', to: "charts#monthly_revenue", via: 'get'
match 'items_sold', to: "charts#monthly_items_sold", via: 'get'
end
end
But this gives me the routes:
product_revenue GET /monthly/products/:product_id/revenue(.:format) charts#monthly_revenue
product_items_sold GET /monthly/products/:product_id/items_sold(.:format) charts#monthly_items_sold
where monthly gets appended in front instead, and the route naming is off. I know I could just do:
resources :products do
match 'monthly/revenue', to: "charts#monthly_revenue", via: 'get', as: :monthly_revenue
match 'monthly/items_sold', to: "charts#monthly_items_sold", via: 'get', as: :monthly_items_sold
end
but that isn't DRY, and it gets crazy when I try to add more categories like yearly. Using a namespace would force me to create a new controller for each namespace, when I want to consolidate all the charts into a single controller.
So I guess the summarised question would be: is it possible to namespace routes without namspacing controllers? Or is it possible to consolidate the creation of categories of named routes?
Edit: Using
resources :products do
scope "monthly", as: :monthly, path: "monthly" do
match 'revenue', to: "charts#monthly_revenue", via: 'get'
match 'items_sold', to: "charts#monthly_items_sold", via: 'get'
end
end
would give me the routes
monthly_product_revenue GET /monthly/products/:product_id/revenue(.:format) charts#monthly_revenue
monthly_product_items_sold GET /monthly/products/:product_id/items_sold(.:format) charts#monthly_items_sold
which similar to the first block, is unexpected because I expect that if a scope is nested in a resources block, only the routes in the scope block would affected by the the scope, not the resources block.
Edit 2: Forgot to include this information earlier, but I'm on Rails 4.0.0, with Ruby 2.0.0-p247
The real solution is to use nested:
resources :products do
nested do
scope 'monthly', as: :monthly do
get 'revenue', to: 'charts#monthly_revenue'
get 'items_sold', to: 'charts#monthly_items_sold'
end
end
end
Ref: https://github.com/rails/rails/issues/12626
Here's how I might approach:
periods = %w(monthly yearly)
period_sections = %w(revenue items_sold)
resources :products do
periods.each do |period|
period_sections.each do |section|
get "#{period}/#{section}", to: "charts##{period}_#{section}", as: "#{period}_#{section}"
end
end
end
It is also possible to use named routes and pass the values to your controller method via params (be sure to properly validate before using):
resources :products do
get ":period/:section", to: "charts#generate_report", as: :report
end
# report_path(period: 'monthly', section: 'revenue')

Abstracting rails route

I want to replace the normal /users/:id route that is created by the resources command, with a more abstract /profile route. It won't be possible to view other users profiles in my app, and therefor the current route is unnecessary specific.
I have tried to overwrite the route created by 'resources :users' with:
get '/profile', to: 'users#show'
and other variances and combinations, but can't seem to get it right. Either the controller can't find the user because of a missing id or it simply can't find the route.
Thanks for the help!
You can use this code in routes.rb file:
resources :users, :except => :show
collection do
get 'profile', :action => 'show'
end
end
It will generate url "/users/profile".
But, if u want to use only '/profile', then don't create route as collection inside users resources block.
resources :users, :except => :show
get 'profile' => "users#show", :as => :user_profile
It will redirect '/profile' to show action in users controller.
I suggest simply adding a users/me route pointing to the show action of your UsersController like so:
resources :users, only: [] do
collection do
get 'me', action: :show
end
end
You can also use the match keyword in routes.rb file.
match 'users/:id' => 'users#show', as: :user_profile, via: :get

Rails custom route not working

Feel like I'm doing this right, but apparently not.
I have a restful resource, Posts, with index, show, new, update, edit, etc actions in the controller. In routes, I have
resources :posts
I wanted to make the index action occur at the URL '/archive' instead of '/posts'
So I added this line in the routes.rb file, after the resources one:
match '/archive', to: "posts#index"
But when I click on a link to posts_path, it still goes to /post (though if I type in /archive as a url, it works -- not ideal, though). Confused. Could this have to do with my having installed friendly_id?
resources :posts, except: [:index]
get 'archive' => 'posts#index', as: :posts
You need to use something like match '/archive', :to => 'posts#index', :as => 'archived'. Then you will have a new route to the tune of archived_posts_path. The method posts_path does not dynamically changed based on custom matchers. You can always run rake routes to see a list of routes for your site.

Resources