0

From the documentation the below route for edit and update will be like /comments/:id

resources :articles do
  resources :comments, shallow: true
end

When I am updating comments and it doesn't know about articles how do you get back to the comments index? Do you get back to the articles index in such a case?

Is there an acceptable work around to get back to the list of comments after updating a comment when using shallow nesting?

Update My models use has_and_belongs_to_many association so I cannot do @comment.article to get the parent.

My models

class Venue < ApplicationRecord
  has_and_belongs_to_many :users
end

class User < ApplicationRecord
  has_and_belongs_to_many :venues
end

My route

resources :venues do
      resources :accounts, shallow: true
end
iJK
  • 4,655
  • 12
  • 63
  • 95
  • 1
    To clarify, when you say "comments index," you mean the list of comments for a particular article (`/articles/:article_id/comments`), right? – Max Dec 10 '18 at 01:29
  • Yes, that is correct. – iJK Dec 10 '18 at 01:34
  • 1
    You can grab the `article_id` in your controller with something like `@article = @comment.article`. Assuming you have your associations set up correctly, a comment that you are trying to update should already have an associated article. – Mark Merritt Dec 10 '18 at 01:47
  • @MarkMerritt I cannot do that if my models are using has_and_belongs_to_many association. – iJK Dec 10 '18 at 01:51
  • So maybe this is a case where shallow nesting is not appropriate. It's only one level deep, so getting rid of shallow nesting would actually make it simpler IMO. As a side note, consider using `has_many :through` instead of habtm. – Mark Merritt Dec 10 '18 at 02:11
  • @MarkMerritt Got it. Thanks! – iJK Dec 10 '18 at 03:31

0 Answers0