In my routes.rb I have:
resources :countries do
resources :cities
end
But I only want:
new: GET /countries/:id/cities/new
create: POST /countries/:id/cities
And not the 7 actions.
What can I do?
Here you go:
resources :cities, :only => [:new, :create]
Reference here.
try
resources :cities, :only => [:new,:create]
Related
I have the following route:
resources :articles, except: [:index] do
collection do
get 'author/:id/articles' => 'articles#index', as: 'index_articles'
end
end
This results in:
GET /api/v1/articles/:id/author/:id/articles(.:format)
How can I turn this into the following?:
GET /api/v1/author/:id/articles(.:format)
# config/routes.rb
resources :articles, except: [:index]
resources :authors, path: "author", only: [] do
resources :articles, only: :index, on: :member #-> url.com/author/:id/articles
end
Write this
get 'author/:id/articles' => 'articles#index', as: 'index_articles'
Outside the resources :articles block.
I have following relationship routes:
resources :courses, only: [:index, :show] do
resources :enrols, only: [:new, :create]
resources :lectures, only: [:show]
end
resources :code_casts, :path => 'casts', :as => 'casts', only: [:index, :show]
resources :blogs, :path => 'blog', :as => 'blog', only: [:index, :show] do
resources :blog_votes, only: [:create, :destroy]
end
I want polymorphic comments in courses, lectures, code_casts and blog.
Problem is lecture has a parent of course so route will be course/course_id/lecture/id and blog path will blog/id where comments will have different show pages.
If I understand the problem correctly, there is nothing special about deeply nested resources. So you might need something like this
# routes.rb
concern :commentable do
resources :comments
end
resources :courses, only: [:index, :show] do
resources :enrols, only: [:new, :create]
resources :lectures, only: [:show], concerns: [:commentable]
end
resources :code_casts, :path => 'casts', :as => 'casts', only: [:index, :show]
resources :blogs, :path => 'blog', :as => 'blog', only: [:index, :show], concerns: [:commentable] do
resources :blog_votes, only: [:create, :destroy]
end
This will create nested comments resources for lectures and blogs.
Than you need to differentiate the path in a comments controller
# comments_controller
def create
Comment.create(commentable: commentable, other_params...) # assuming you have `commentable` polymorphic belongs_to
end
# a little more ugly than Ryan suggests
def commentable
if request.path.include?('blog') # be careful. any path with 'blog' in it will match
Blog.find(params[:id])
elsif params[:course_id] && request.path.include?('lecture')
Course.find(params[:course_id).leactures.find(params[:id]) # assuming Course has_many lectures
else
fail 'unnable to determine commentable type'
end
end
All 'magic' is in commentable method, where youare checking the path and determine which commentable object to pick. I use similar approach, but this exact code is written by memory without testing. Hopefully you've got the idea.
How do I add the /search endpoint to all these resources in one go?
MyCoolApp::Application.routes.draw do
resources :posts, only [:index, :show, :create] do
collection { get :search }
end
resources :authors, only [:index, :show] do
collection { get :search }
end
resources :comments, only: [:index, :show] do
collection { get :search }
end
resources :categories, only: [:index, :show] do
collection { get :search }
end
resources :tags, only: [:index] do
collection { get :search }
end
end
I saw this answer which is close, but I need to be able to specify the actions for reach resource. Is there a better way to inject the search route in a better way?
%w(one two three four etc).each do |r|
resources r do
collection do
post 'common_action'
end
end
end
Something like this?
resources :posts, only [:index, :show, :create, :search]
I know you've tagged this question ruby-on-rails-3, but I'm going to provide the Rails 4 solution for anyone who comes across this in the future.
Rails 4 introduces Routing Concerns
concern :searchable do
collection do
get :search
end
end
resources :posts, only [:index, :show, :create], concerns: [:searchable]
resources :authors, only [:index, :show], concerns: [:searchable]
resources :comments, only: [:index, :show], concerns: [:searchable]
resources :categories, only: [:index, :show], concerns: [:searchable]
resources :tags, only: [:index], concerns: [:searchable]
Good day all,
Can someone kindly assist me with nested resources and its best practice.
I would like to restrict my :events route to only :show and :index, is this the correct way of doing it?
resources :events do
resources :registrations, :except => [:index]
end
resources :events, :only => [:index, :show]
Is this the best way or the way more Rubyist would handle such thing? I've added two lines of resources :events or is there a way to combine it all in 1 block?
Thanks in advance.
Yes. You can combine it in one block like:
resources :events, only: [:index, :show] do
resources :registrations, except: :index
end
I have this in my routes.rb
resources :users, :path => "/", :only => [:show] do
resources :collections, :controller => 'users/collections'
end
Why can I access to collections resources from:
http://localhost:3000/en/collections
if this resouce this inside users?
I want that collection resource only is enable inside:
http://localhost:3000/en/users/collections
This is my routes:
user_collections GET (/:locale)/:user_id/collections(.:format) users/collections#index
POST (/:locale)/:user_id/collections(.:format) users/collections#create
new_user_collection GET (/:locale)/:user_id/collections/new(.:format) users/collections#new
edit_user_collection GET (/:locale)/:user_id/collections/:id/edit(.:format) users/collections#edit
user_collection GET (/:locale)/:user_id/collections/:id(.:format) users/collections#show
PUT (/:locale)/:user_id/collections/:id(.:format) users/collections#update
DELETE (/:locale)/:user_id/collections/:id(.:format) users/collections#destroy
How can I do it?
Thank you
Try this:
resources :users, :only => [:show] do
resources :collections, :controller => 'users/collections'
end