Rails routing error when using resource but now when using GET - ruby-on-rails

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.

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.

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

Related

No Route Matches for a very basic Rails4 App

I'm very new to RoR and I'm trying to get a very basic site going. I have my home page working okay, but when I try to add a new page, I keep getting "No route matches" in the log. Here is the output from rake routes:
Prefix Verb URI Pattern Controller#Action
inventing_index GET /inventing/index(.:format) inventing#index
ideas_index GET /ideas/index(.:format) ideas#index
root GET / ideas#index
However, when I go to mysite.com/inventing or mysite.com/inventing/index I get the no route matches error. mysite.com/ shows the app/views/ideas.erb as hoped. All I did was rails generate controller inventing index. Is there something else I have to do to activate the route?
I'm running ruby 2.0.0p247 and rails 4.0.0 with passenger/apache on centos 6. I installed all the ruby/rails/passenger stuff, so its possible something isn't setup properly.
Thanks
EDIT: Here is my routes.db file:
Rortest::Application.routes.draw do
get "inventing/index"
get "ideas/index"
root to: 'ideas#index'
end
tldr: Your problem is probably the route. Change get 'inventing/index' to get 'inventing/index'=> 'inventings#index' to correctly point the route to your controller action.
There are four things you need to do when adding a new page/route. I usually do them in this order for static pages:
1) Create a controller with the appropriate action for each page
You already did this with rails generate controller inventing index, but make sure that:
In your app/controllers folder, you do indeed have a file called inventings_controller.rb
In inventings_controller.rb, you have at least this:
class InventingsController < ApplicationController
def index
end
end
2) Create a view for each controller action
Make sure that:
In your app/views/inventings folder, you have a file called index.html.erb
Puts some simple HTML in that file, like <h1>Testing123</h1>, just so you can see if it's working.
3) Add a route for each controller action
It looks like this may be the problem. In your routes.rb file, you'll need this:
get 'inventing/index' => 'inventings#index'
Your root to works because it's actually pointing directly to your controller action, but rails isn't smart enough to guess that inventing/index should go to the index action in your inventing**s** controller.
4) As others have suggested, restart your rails app
You can do this with ctrl+c at the command line, then rails s again to start it back up.

catching wild card route for missing routes, doesn't work when having additional routes files

I implemented a simple custom errors solution.
this one: http://ramblinglabs.com/blog/2012/01/rails-3-1-adding-custom-404-and-500-error-pages
everyhing is working fine except the missing routes in the routes.rb file..
in order to get to my error_controller when there is a missing route i did the wildcard solution: match '*not_found', to: 'errors#error_404'
but... now when i try to enter a sub section of my site which seats under:
/admin, i get to the error page. the wilcard gets triggered, even tough the route for admin section is defined in a different route file, under: config/routes/admin.rb
what can I do?
thanks
edit:
using rails 3.0.20 and ruby 1.8.7
If you're using Rails 3.2+, there is a simpler solution for your routes. First in 'config/application.rb' set your app as the error handler
config.exceptions_app = self.routes
Now when there is an your app will look to your routes to handle it. In 'config/routes.rb' you can add a route such as:
match "/404", :to => "errors#not_found"
A more verbose explanation can be found here.
OK so until I will update to Rails 3.2+
I simply put '*not_found', to: 'errors#error_404' into the last route file that is loaded.
that way its truly in the end of the routes and now all my routes work. and the error is still fired when needed.

Resources-Route with nested GET breaks Resource-Routes?

Running Rails 3.2.11, I have a regular controller "LbuController" which is basically an enhanced scaffold, enhanced by 4 additional GET methods used for AJAX requests.
resources :lbus do
get 'add_offering'
get 'remove_offering'
get 'add_offering_element'
get 'remove_offering_element'
end
running rake routes gives me the following routes
lbu_add_offering GET /lbus/:lbu_id/add_offering(.:format) lbus#add_offering
lbu_remove_offering GET /lbus/:lbu_id/remove_offering(.:format) lbus#remove_offering
lbu_add_offering_element GET /lbus/:lbu_id/add_offering_element(.:format) lbus#add_offering_element
lbu_remove_offering_element GET /lbus/:lbu_id/remove_offering_element(.:format) lbus#remove_offering_element
lbus GET /lbus(.:format) lbus#index
POST /lbus(.:format) lbus#create
new_lbu GET /lbus/new(.:format) lbus#new
edit_lbu GET /lbus/:id/edit(.:format) lbus#edit
lbu GET /lbus/:id(.:format) lbus#show
PUT /lbus/:id(.:format) lbus#update
DELETE /lbus/:id(.:format) lbus#destroy
which seem exactly to be what I intended.
But following a link to http://localhost:3000/lbus/new created with link_to "new", new_lbu_path gives me the following error:
No route matches {:action=>"add_offering", :lbu_id=>nil, :offering_id=>1, :controller=>"lbus"}
which makes absolutely no sense for me.
Anybody got any ideas what's happening here and what's going wrong?
Thanks in advance!
On the 'new' view you must have used a lbu_add_offering path.
That path seems to be incorrect. The error refers to that :action=> "add_offering".

Rails redirect 301

I have some touble with redirect 301 in my new app. I have to redirect some old urls into the new one.
I entred in my routes file this
match "/traslochi_puglia/index.htm", :to => redirect("/preventivo/90-traslochi-in-puglia")
and it works fine, but I can't understand why this
match "/trasloco_casa_abitazione.htm", :to => redirect("/3-trasloco-casa")
does not work. All the old urls with this pattern "/some_path/page.htm" works fine but not "page.htm". Any hint?
Thanks
If you want us to troubleshoot the specific issue you've outlined in your question, we need to see your entire routes.rb file. Without this information, my first guess is this:
The typical route pattern is /controller/action or /controller/:id/action or some combination thereof. With the pattern you've shown above, and assuming you have no named routes in your routes.rb file, then the route you've provided would point to a controller, but not an action. Therefore your app wouldn't know what action to execute, unless you've specifically created a route called /3-trasloco-casa which looks to me more like a URL to a specific resource, than an action on a controller.
Getting to the source of routing issues can most easily be done with a combination of running rake routes at the command line (which shows you the list of route patterns your app will recognize), and then going further by troubleshooting with route recognition, as outlined in this answer to this question:
Recognize routes in rails console Session

Beginner RoR Error After Generating Views (RailsSpace)

I'm using RailsSpace to learn Ruby on Rails and am coming across an error after performing what seems like a simple command.
I used the Terminal to generate a new User controller with the views Index and Register:
$ rails generate controller User index register
And it had no problem with that, creating the files index.html.erb and register.html.erb as well as all the other expected files.
But when I visit http://localhost:3000/user/register, it comes back with the error message:
ROUTING ERROR: No route matches {:controller=>"user",
:action=>"about"}
My routes.rb doesn't indicate any abnormalities:
RailsSpace::Application.routes.draw do
get "user/index"
get "user/register"
get "site/index"
get "site/about"
get "site/help"
root :to => "site#index"
end
Why does it try to route to the "About" action, and what other file can I edit to change this routing?
I'm using Rails 3 in case that matters.
I would try hand coding in the route. In your case it would look like this:
match '/user/register' => 'users#register', :as => :register
This will definitely work and prevent the page /user/register from going to the about page. Let me know how things go and Ill try to continue to steer you in the right direction.

Resources