andersr
andersr

Reputation: 1051

Rails routing error when using resource but now when using GET

I am creating a simple suggestion box app (to learn Rails) and am getting the following Rails routing error when I go to "/suggestion-boxes" running on my local machine (localhost:3000)

"Routing Error

No route matches [GET] "/suggestion-boxes"

In my routes.rb file I have:

SuggestionBoxApp::Application.routes.draw do
  resources :suggestion_boxes
end

This is what I get when I run rake routes:

suggestion-box-app$ rake routes
suggestion_boxes GET    /suggestion_boxes(.:format)          suggestion_boxes#index
                POST   /suggestion_boxes(.:format)          suggestion_boxes#create
new_suggestion_box GET    /suggestion_boxes/new(.:format)      suggestion_boxes#new
edit_suggestion_box GET    /suggestion_boxes/:id/edit(.:format) suggestion_boxes#edit
 suggestion_box GET    /suggestion_boxes/:id(.:format)      suggestion_boxes#show
                PUT    /suggestion_boxes/:id(.:format)      suggestion_boxes#update
                DELETE /suggestion_boxes/:id(.:format)      suggestion_boxes#destroy

However, if I modify my routes file to

SuggestionBoxApp::Application.routes.draw do
  get "suggestion-boxes" => "suggestion_boxes#index"
end

Then the page "/suggestion-boxes" displays as per the index action in my SuggestionBoxesController.

I tried restarting my server but this had no impact. While I of course can go with using GET, this error makes no sense, and I would like understand what is causing it.

Any insights would be very much appreciated.

Upvotes: 0

Views: 97

Answers (2)

r4m
r4m

Reputation: 491

The error is that you are not renaming the REST route, instead the controller one. Try declaring

resources :suggestion_boxes, :as => "suggestion-boxes"

in your config/routes.rb file.

Upvotes: 2

Antarr Byrd
Antarr Byrd

Reputation: 26169

Somewhere in your code you are calling to suggestion-boxes controller which does not exist. Your controller is suggestion_boxes, spelling. So where every you have "suggestion-boxes" you should replace with "suggestion_boxes". The code that you added create an alias that matches 'suggestion-boxes' to the index action of the suggestion_boxes controller so this resolves it if it is your desired affect. But simply fixing your spelling would resolve your problem. I usually use the second approach if I want the change the URL that user see. Have a look at the routing doc for a better understanding

Upvotes: 1

Related Questions