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]
Related
In my routes.rb file, I have the following resources
resources :classrooms, only: [:index, :new, :create] do
resources :students, only: [:index, :edit, :update], controller: 'students' do
get :honor
end
end
My honor route becomes /classrooms/:classroom_id/students/:students_id/honor(.:format)
but I want to replace student_id to id so that I can easily use it in the students controllers.
Ideal route is /classrooms/:classroom_id/students/:id/honor(.:format)
You can do that specifying it's a member route. this way
get :honor, on: :member
or
member do
get :honor
end
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.
I am trying to combine collection and member in the conversations path. But I could not figure it out,
resources :conversations, only: [:index, :show, :destroy] do
member do
post :reply
post :restore
end
end
and;
resources :conversations, only: [:index, :show, :destroy] do
collection do
delete :empty_trash
end
end
When I combine them it does not work, and obviously this one is wrong too!.
Combine member and collection in resources block. Like this,
resources :conversations, only: [:index, :show, :destroy] do
member do
post :reply
post :restore
end
collection do
delete :empty_trash
end
end
Or you may combine it like this also,
resources :conversations, only: [:index, :show, :destroy] do
post :reply, on: :member
post :restore, on: :member
delete :empty_trash, on: :collection
end
try this
resources :conversations, only: [:index, :show, :destroy] do
member {
post :reply
post :restore
}
collection {
delete :empty_trash
}
end
I am using devise and therefore do not need a users controller.However, i also need nested routes and my config.routes looks like this;
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
devise_for :users
resources :users do
resources :personal_accounts,path: "user_account", only: [:show] do
resources :deposits, only: [:new, :show, :create, :index]
resources :withdraws, only: [:new, :show, :create, :index]
end
resources :businesses do
resources :business_accounts, path: "business_account", only: [:show] do
resources :business_withdraws, only: [:new, :show, :create, :index]
resources :business_deposits, only: [:new, :show, :create, :index]
end
end
end
How can i go past this error while also maintaining my nested routes.
Thank you.
You have three levels of nested routes there, which is normally considered to be undesirable: http://edgeguides.rubyonrails.org/routing.html#nested-resources
Resources should never be nested more than 1 level deep.
This bit resources :users do will create all the named routes for the users controller, which I suspect is where your error comes from. Why do you need this? Better perhaps to specify the routes without it?
resources :personal_accounts,path: "user_account", only: [:show] do
resources :deposits, only: [:new, :show, :create, :index]
resources :withdraws, only: [:new, :show, :create, :index]
end
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