Search code examples
ruby-on-railsruby-on-rails-3routesnestednested-routes

Nested route issue: "No route matches [GET] "/questions/6/responses"


I'm a rails beginner struggling with routes a bit. I'm working on a Q&A site (or question and response) and any time I try to post a response, it gives the following error:

No route matches [GET] "/questions/6/responses"

Any help would be greatly appreciated, Jon

Here are the relevant bits:

Routes output (given w the error) (apologies for nasty paste):

Helper  HTTP Verb   Path    Controller#Action
Path / Url          
welcome_index_path  GET /welcome/index(.:format)    welcome#index
users_path  GET /users(.:format)    users#index
POST    /users(.:format)    users#create
new_user_path   GET /users/new(.:format)    users#new
edit_user_path  GET /users/:id/edit(.:format)   users#edit
user_path   GET /users/:id(.:format)    users#show
PATCH   /users/:id(.:format)    users#update
PUT /users/:id(.:format)    users#update
DELETE  /users/:id(.:format)    users#destroy
categories_path GET /categories(.:format)   categories#index
POST    /categories(.:format)   categories#create
new_category_path   GET /categories/new(.:format)   categories#new
edit_category_path  GET /categories/:id/edit(.:format)  categories#edit
category_path   GET /categories/:id(.:format)   categories#show
PATCH   /categories/:id(.:format)   categories#update
PUT /categories/:id(.:format)   categories#update
DELETE  /categories/:id(.:format)   categories#destroy
question_responses_path POST    /questions/:question_id/responses(.:format) responses#create
questions_path  GET /questions(.:format)    questions#index
POST    /questions(.:format)    questions#create
new_question_path   GET /questions/new(.:format)    questions#new
edit_question_path  GET /questions/:id/edit(.:format)   questions#edit
question_path   GET /questions/:id(.:format)    questions#show
PATCH   /questions/:id(.:format)    questions#update
PUT /questions/:id(.:format)    questions#update
DELETE  /questions/:id(.:format)    questions#destroy
root_path   GET /   welcome#index

Routes:

Rails.application.routes.draw do
  get 'welcome/index'

resources :users

resources :categories

resources :questions do
  resources :responses, :only => [:create]

end

root 'welcome#index'

Questions controller https://github.com/joncarpe/snack/blob/master/app/controllers/questions_controller.rb

Responses controller https://github.com/joncarpe/snack/blob/master/app/controllers/responses_controller.rb


Solution

  • You have only route to create action in ResponsesController. If you also want route to index, you should have:

    resources :responses, only: [:create, :index]
    

    If you want routes to all default resources actions, you should abandon only option, like this:

    resources :responses