简体   繁体   中英

Rails - Routes for Nested Resources

I have a conversations controller and a comments controller.

What I would like to do is have the following (from the logs):

Started POST "/conversations/217/comment_beta" for 127.0.0.1

Post the Comments Controller not the Conversations Controller which is what Rails is trying to do now:

AbstractController::ActionNotFound (The action 'comment_beta' could not be found for ConversationsController):

Here is my routes file:

  resources :conversations do
    resources :comments, :only => [:create, :update,:destroy, :comment_beta], :constraint => {:context_type => "conversations"} do
      collection do
        post 'comment_beta'
      end
    end
    collection do
      get 'read_updater'
    end
  end

Suggestions? Thanks

your rails routes is actually doing what it is supposed to do. if you conversations/:id/comment_beta to go to your comments controller, either you should change your routes via match or go to the correct url which is /conversations/:id/comments/:comment_id/comment_beta

If you're posting to create a new comment, why aren't you using RESTful routes?

resources :conversations do
  resources :comments do
    collection do
      post 'comment_beta'
    end
  end
end

should give you /conversations/:id/comments/comment_beta

collection because you don't need an id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM