sb2uk
sb2uk

Reputation: 21

No route matches {:controller=>"comments", :action=>"create"} - It does

Edit: I've seen a number of these but couldn't find an answer to this so I'm attempting to document it as best I can and asking this question.

I have a model-less rails app (calling an API) with a nested comments resource. I am able to post a comment against a story if I go directly to the comments#new or comments#index action and accordingly post to the comments#create action.

However I'd like very much to be able to post a comment on the same page as the #show action of the parent resource: (opusses#show)

I've tried using the rails url_helper path from rake routes as opuss_comments_path and explicitly stating the controller and action. In both cases I still get this message:

No route matches {:controller=>"comments", :action=>"create"}

Here is my routes db:

resources :users
resources :sessions,  only: [:new, :create, :destroy]
resources :osessions, only: [:new, :create, :destroy]

resources :authors do
  member do
    get   :following
    get   :followed
    post  :follow
  end
end

resources :opusses do

  resources :comments

  member  do
    get   :like
    get   :authorfeed
    post  :repost
  end
end

And my Rake Routes:

                   DELETE /authors/:id(.:format)                         authors#destroy
    opuss_comments GET    /opusses/:opuss_id/comments(.:format)          comments#index
                   POST   /opusses/:opuss_id/comments(.:format)          comments#create
 new_opuss_comment GET    /opusses/:opuss_id/comments/new(.:format)      comments#new
edit_opuss_comment GET    /opusses/:opuss_id/comments/:id/edit(.:format) comments#edit
     opuss_comment GET    /opusses/:opuss_id/comments/:id(.:format)      comments#show
                   PUT    /opusses/:opuss_id/comments/:id(.:format)      comments#update
                   DELETE /opusses/:opuss_id/comments/:id(.:format)      comments#destroy

&&

        like_opuss GET    /opusses/:id/like(.:format)                    opusses#like
  authorfeed_opuss GET    /opusses/:id/authorfeed(.:format)              opusses#authorfeed
      repost_opuss POST   /opusses/:id/repost(.:format)                  opusses#repost
           opusses GET    /opusses(.:format)                             opusses#index
                   POST   /opusses(.:format)                             opusses#create
         new_opuss GET    /opusses/new(.:format)                         opusses#new
        edit_opuss GET    /opusses/:id/edit(.:format)                    opusses#edit
             opuss GET    /opusses/:id(.:format)                         opusses#show
                   PUT    /opusses/:id(.:format)                         opusses#update
                   DELETE /opusses/:id(.:format)                         opusses#destroy

When I call the code below from comments#index page it works perfectly. However it's quite common to post to another form from a different controller and when I call this code from the opusses#show page it fails with the error above.

On the off chance it had to do with the URL helper, I tried specifying the controller and action explicitly and that still didn't work - generated the same error.

Upvotes: 0

Views: 1281

Answers (2)

sb2uk
sb2uk

Reputation: 21

Classic newbie mistake, but for others benefit =>

I had rake routes and I had the path correct, what I wasn't doing was submitting a the id of the parent resource. So POST to the path and include the object in question. In my case this mean opuss_comments_path(@opuss["xyz"]) where xyz was the id of my object.

opuss_comments GET    /opusses/:opuss_id/comments(.:format)          comments#index
               POST   /opusses/:opuss_id/comments(.:format)          comments#create

Ah.. learning. :)

Upvotes: 2

ADAM
ADAM

Reputation: 3903

Based on your routes, You shouldn't have to use a url helper. but you do have to make sure that you have a handle on the Opuss object in the controller. so do something like this ;

@opuss = Opuss.find(params[:id]) #or your equivalent finder code
@comment = @opuss.comments.build

and then in your view;

<%= form_for([@opuss, @comment]) do |f| %>
  .... rest of form
<% end %> 

Upvotes: 1

Related Questions