rails routing nested resources duplicate - ruby-on-rails

I want to support:
POST images/1/comments/2/like
and
POST comments/2/like
They both point to same resources with same action. How can I do that in rails route file?
resources :images do
resources :comments do
member do
post 'like'
end
end
end
This will work for
POST images/1/comments/2/like
but how can I also for when I don't specify the images/1 part?

You can make it more beautiful actually. According to http://ruby-journal.com/how-to-dry-your-rails-routes/, this also works:
comments = Proc.new do
member do
post 'like'
end
end
resources :comments, &comments
resources :images do
comments.call
end
and in Rails 4 you could use concerns
concern :comments_concern do
member do
post 'like'
end
end
resources :comments, concerns: :comments_concern
resources :images, concerns: :comments_concern do
#do more stuff here
end
I didn't test this, but it might help. Have a look at the website mentioned. Good luck
-frbl

I would consider reworking which RESTful routes for Comment you're nesting, and which you're not. I'm assuming your models look something like this:
# app/models/image.rb
class Image < ActiveRecord::Base
has_many :comments
end
# app/models/comment.rb
class Comment < ActiveRecord::Base
belongs_to :image
end
Because your Image and Comment models possess a one-to-many relationship, I can see why you'd think to nest the comments resource route within the images one. However, of all the CRUD actions in comments_controller.rb, only create actually requires that a parent image ID be explicitly passed in. From a RESTful perspective, only the new and create actions require that a image_id is passed to the action. The edit, update, delete, and like actions can all take place independently of the parent image.
Consider the alternative routing schematic instead:
# config/routes.rb
resources :images do
resources :comments, :only => [:index, :new, :create]
end
resources :comments, :only => [:show, :edit, :update, :destroy] do
member do
post 'like'
end
end
Now, only the comment actions that are explicitly depended on a parent ID are actually nested within the images routes. The remainder of the comment actions are directly routed to the comments controller without passing in the parent ID. Your routes are no longer duplicated, and each action will have exactly one route declared for it.

simply add resources :images it may work

Not sure there is a much more beautiful way than repeating this below:
resources :comments do
member do
post 'like'
end
end
Like so:
resources :images do
resources :comments do
member do
post 'like'
end
end
end
resources :comments do
member do
post 'like'
end
end

Related

Add rails routing concerns before the resource

I'm wanting to make a product resource :locationable (meaning, can be filtered by its location).
# routes.rb
concern :locationable do
member do
get 'location/:location_id'
end
end
resources :products, concerns: :locationable, action: :index
The routes above create the following route:
/products/location/:location_id
However, I'm wanting it to put the location first in the route. For example:
/location/:location_id/products
I'm wanting to use concerns for this--not nested resources.
What about changing it to
# routes.rb
concern :locationable do
resources :products, only: :index
member do
get 'location/:location_id'
end
end
Check out routing concerns from here https://gist.github.com/dideler/10020345.

Set the scope based on parent resource while using concerns

I am curious, is there any way to set the scope for nested resources based on parent resource while using concerns in Rails?
concern :commentable do
scope module: ??? do # either :posts or :messages
resources :comments
end
end
resources :messages, concerns: :commentable
resources :posts, concerns: :commentable
I want my routes to be like these:
Path Controller
/messages/:message_id/comments/:id messages/comments#show
/posts/:post_id/comments/:id posts/comments#show
but I also want to use concerns in order to reduce duplication.
Thanks!
Turns out method concerns can take a hash of options.
concern :commentable do |options|
scope module: options[:module] do
resources :comments
end
end
resources :messages do
concerns :commentable, module: :messages
end
resources :posts do
concerns :commentable, module: :posts
end
I started with your solution using an options hash but it turns out I really need this pattern a lot so I worked out another solution for that specific case :
concern :archivable do
scope module: parent_resource.plural do
resources :archiving, only: [:create, :destroy]
end
end
...
resources :messages , concerns: [ :archivable ]
resources :users , concerns: [ :archivable ]
You can use Rails Polymorphism to fit this situation where a comment can belong_to any other model as well...post,video,user etc
Wonderful Railscasts.....which is worth watching.

Appropriately nest resources in routes.rb

My application has deeply nested resources, but after reading http://guides.rubyonrails.org/routing.html#shallow-nesting, I've realized it's not wise to have these deeply nested resources. Here is the situation my routes are currently in.
resources :assortments do
resources :comments do
member do
post :like
post :unlike
end
end
member do
post :like
post :unlike
end
resources :designs do
resources :comments do
member do
post :like
post :unlike
end
end
member do
post :like
post :unlike
get :likes
end
end
end
I want to know the proper way to organize this structure to make it less confusing and "proper". Or is what I have ok?
Thanks.
you could just specify the shallow: true on your top resources :assortments and should build shallow routes for all the nested routes automatically.
I also would move the :comments resource into a concern, kind of like that
concern :commentable do
resources :comments do
member do
:like
:unlike
end
end
end
resources :assortments, shallow: true do
concerns :commentable
...
resources :designs do
concerns :commentable
end
end
And on an unrelated topic, i'd consider making the LikesController to have a centralized likes handling resource, might save one some headache when you have polymoriphic liking thing going on.

Custom Nested Routes

I am trying to get custom routes based on the username rather than the ID. I have it working to get to the show page of the user but I am also trying to nest the resources so that I can see his posts and comments using the same syntax.
Example:
Works... "mysite.com/users/username/"
Does not work... "mysite.com/users/username/posts/"
routes.rb
...
# Users with the Username...
match 'users/:username' => "users#show" do
get :posts
get :comments
end
# Users with the ID...
resources :users do
get :posts
get :comments
end
...
Perhaps you can use the to_param method and update your nested routes/resources:
routes.rb:
resources :users do
resources :posts
resources :comments
end
user.rb
class User < ActiveRecord::Base
def to_param
username
end
end
..for finds in the UserController:
#user = User.find_by_username(params[:id])
(or any variation of finding by the username criteria)

Exposing a Specific Action of a Nested Route

Using this as an example contexted:
Post has_many comments
Comment belongs_to post
I have a route that looks like:
resources :posts do
resources :comments
end
How do i create a route that exposes comments#index?
An example use case would be... I want to list ALL comments in the system on a page. Essentially using the comments resource as if it's not nested when a user hits /comments
thank you!
Try this.
resources :posts do
resources :comments, :except => :index
end
match 'comments' => 'comments#index', :as => :comments
That said, I usually look to avoid routes like this because I like a tidy RESTful routes file, but sometimes it can't be helped.
Second option:
resources :posts do
resources :comments, :except => :index
get :comments, :on => :collection
end
In the second option, you'd want to remove the index action from the comments controller and create a comments action in your posts controller.

Resources