I have an app with a parent resource (has_many) of patterns and a child resource (belongs_to) of snippets. My desire is to build a couple custom routes to specific pages and I am wondering the best way to do that. For now, here is what I have that is working, but I am wondering if there is a better way since the article I read on custom routing tells me this is not good practice.
I have purposefully not nested the resources because the snippets need to stand alone as well as be viewed within the context of their parent pattern.
The goal is to be able to create a few custom views like the following:
http://patterns.dev/patterns/:id/snippets //Got this one working, but incorrectly I believe
http://patterns.dev/patterns/:id/snippets/sort // Show all snippets for pattern to sort
http://patterns.dev/patterns/:id/images // Show all the images for patterns to moderate
routes.rb
Rails.application.routes.draw do
devise_for :users, :path => '', :path_names => {:sign_in => 'login', :sign_out => 'logout'}
resources :patterns
get 'patterns/:id/snippets' => 'patterns#snippets', as: 'pattern_snippets'
resources :snippets
root 'welcome#index'
end
I guess nested resources is what you need. You can specify just index action for nesting and keep all the snippets resources separately. Also you are to add some logic to snippets#index action to check params[:pattern_id]'s existance:
resources :patterns do
# this line will generate the `patterns/:id/snippets` route
# referencing to snippents#index action but within specific pattern.
# Also you have to add pattern_id column to snippets table to define the relation
resources :snippets, only: :index
end
resources :snippets
Use Collection Routes to make Rails to recognize paths like /patterns/:id/snippets/sort
resources :patterns do
resources :snippets, only: :index do
# this line will generate the `patterns/:id/snippets/sort` route
# referencing to snippets#sort action but again within specific pattern.
get :sort, on: :collection
end
end
resources :snippets
If you have Image model you can nest resources the same way like with snippets:
resources :patterns do
resources :images, only: :index
end
If it's just an action in patterns controller you can do:
resources :patterns do
get :images, on: :member
end
Related
I have the following routes defined:
resources :users do
resources :suggestions do
post :make, on: :collection
end
end
I would like to tell resources :suggestions not to create any of the 7 default actions because I don't need them right now. With :only and :except I can limit the number of those actions being created, but I don't seem to figure how to create none of them.
Indeed, I could just leave the resources out and just define my :make route, but I would like to benefit from the automatic nesting and param handling, as well as leave a door open for a future need of several of the 7 default actions.
Hope this is helpful:-
resources :suggestions, only: [] do
end
I have read up on the Rails Guides.
What I want to set up are the following routes that are routed to the 'profiles' controller:
GET profiles/charities - Should display all the charities
GET profiles/charties/:id should display a specfic charity
GET profiles/donors - Should display all the donors
GET profiles/donors/:id - Should display a specfic donor
I have created the profile controller and two methods: charities and donors.
Is this all I need?
The following will set up routes for what you want, but will map them to :index and :show of CharitiesController and DonorsController:
namespace :profiles do
# Actions: charities#index and charities#show
resources :charities, :only => [:index, :show]
# Actions: donors#index and donors#show
resources :donors, :only => [:index, :show]
end
When it's more appropriate to set up custom routes, something like this would do:
get 'profiles/charities', :to => 'profiles#charities_index'
get 'profiles/charities/:id', :to => 'profiles#charities_show'
get 'profiles/donors', :to => 'profiles#donor_index'
get 'profiles/donors/:id', :to => 'profiles#donor_show'
Here are relevant sections in the guide that you were going through:
Resource Routing: the Rails Default - Controller Namespaces and Routing
Non-Resourceful Routes - Naming Routes
The charities and donors seem to be nested resources. If so, in your config/routes.rb file you should have something like,
resources :profiles do
resources :charities
resources :donors
end
Because these are nested resources, you do not need the two methods named charities and donors in your profiles controller. In fact, depending on your app, you may need separate controllers and/or models for your charities and donors.
Currently I have something like this:
resources :books do
collection do
get 'search'
end
end
and my controller name is also "books" and I have a action method called "search" inside it
I would like that "get search" part to also be a resource, kind of like nested resources... but I don't want to break other peoples codes that are using the current route that this generate, so need to update it in a passive way!
resources :books do
collection do
get 'search'
end
resources :searches
end
...if I'm understanding you correctly, that should be what you want. It won't break other routes, just add new ones.
Run rake routes to make sure you have all the routes you want/need.
Use shallow routes nesting like:
resources :books , :shallow => true do
resources :searches
end
Now you will get the following routes:
/books/1 => books_path(1)
/books/1/searches => books_searches_index_path(1)
/searches/2 => searches_path(2)
Similarly you can get separate routing for defined routes like:
get '(:books)/searches', :to => 'books#index'
How do I rename some of these routes...for example, below i want to use signup_path in my controllers instead of signup_sessions_path...
resources :sessions, only: [] do
collection do
post :signup, :as => :signup
post :login
delete :logout
end
end
Try not to nest the routes under resources :sessions but rather use the to: option like so:
post :signup, to: 'sessions#signup', as: :signup, on: :collection
Not too sure about your collection there but I'm sure you get the gist of it
Update
According to your comment, as of today, I don't know of any way to remove the nested route resource name from the path name of a nested resource route. In other words, whatever is nested is purposely to use the scope of the resource and therefore there are no options to revert that behaviour other than taking it out of the resource's block.
I'm using some routes with scopes, and some without. See below.
resources :cities
resources :categories
devise_for :clients
namespace :clients do
resources :account
resources :dashboard
resources :offers
end
scope "/:current_city" do
scope "/:current_category" do
match 'articles/last_articles' => 'articles#index_last_articles', :as => "last_articles"
resources :articles do
resources :comments
end
end
end
root :to => "home#index"
I am using that params :current_city and :current_category and it gives me a URL like
http://localhost:3000/warszawa/all/articles/last_articles when I'm accessing articles.
**PROBLEM**
I have now such a problem that if I click on a link_to cities_path or root_path, then it adds those two parameters to the URL as http://localhost:3000/?current_category=all¤t_city=warszawa.
I don't want these two parameters destroying the beauty of my URL :o(
The only way I found was to pass :current_city => nil, :current_category => nil for each link, but that's really heavy. I tried also the same but in my routes, which is working for normal resources, but not for namespaces, root_path, devise_for routes, and to be honest, that looks horrible in the routes.rb.
**QUESTIONS**
First I do not understand why these params are passed everywhere if I ask them only in the section with scope?!
Secondly, is there a way to make it work like I want or I should modify my routes?
I wish you understand my problem and if you have any comment or idea, please do not hesitate!
Thx