rake route - No route matches [GET] "/" Error - ruby-on-rails

On visiting localhost:3000 I'm getting the typical:
No route matches [GET] "/"
I don't want to see that error screen. I already trying with localhost:3000/passbook/v1/passes/ but still nothing so I typed:
rake routes
and got this output:
passbook GET /passbook/v1/passes/:pass_type_identifier/:serial_number(.:format) passbook/passes#show {:pass_type_identifier=>/([\w\d]\.?)+/}
GET /passbook/v1/devices/:device_library_identifier/registrations/:pass_type_identifier(.:format) passbook/registrations#index {:pass_type_identifier=>/([\w\d]\.?)+/}
POST /passbook/v1/devices/:device_library_identifier/registrations/:pass_type_identifier/:serial_number(.:format) passbook/registrations#create {:pass_type_identifier=>/([\w\d]\.?)+/}
DELETE /passbook/v1/devices/:device_library_identifier/registrations/:pass_type_identifier/:serial_number(.:format) passbook/registrations#destroy {:pass_type_identifier=>/([\w\d]\.?)+/}
passbook_log POST /passbook/v1/log(.:format) passbook/logs#create
How can I resolve this?

You haven't told Rails yet what your root path is. You can do this by saying
root to: "passes#show"
in config/routes.rb. Afterwards, you should see this in the rake routes output:
root / passes#show
Then it should work!
Additional tip
This may be over the top for your question, but following the test driven approach you should write a spec first:
describe "custom routing" do
it "shows the root page" do
get("/").should route_to("passes#show")
end
end
Use RSpec for this. Run the test in your console before having written the route itself by doing $ rspec and see the test fail at the right place.
Then, implement the routes above, run the test again—it should work. This way you ensure that you have written just enough code to meet your requirements, and that it is really the code you have written that lets you access the root path.

Is this a rails app you inherited from someone else?
Given the rake routes output you have provided, a url like localhost:3000/passbook/v1/passes/<EXAMPLE_PASS_TYPE_IDENTIFIER>/<EXAMPLE_SERIAL_NUMBER> should work based on the rule in the first line of the output, provided you replace EXAMPLE_PASS_TYPE_IDENTIFIER and EXAMPLE_SERIAL_NUMBER with valid values from the database.
The error message No route matches [GET] "/ for localhost:3000 is because there is no mapping for root in rake routes output.
You could modify the config/routes.rb file and add the following line at the bottom of the file:
root :to => "<controller_name>#<action_name>"
Eg:
root :to => "passbook/index"
if there is a passbooks_contoller.rb with a index method.
Best to learn more about rails routing from the documentation. Even better go through the Rails Tutorial to get a good understanding of the rails framework.

Related

Why do I get "Routing error" even though my path is in "rake routes"?

I’m using Rails 5. I have this in my rake routes
localhost:sims nataliab$ rake routes
Prefix Verb URI Pattern Controller#Action
scenario_confidential_memos GET /scenarios/:scenario_id/confidential_memos(.:format) confidential_memos#index
POST /scenarios/:scenario_id/confidential_memos(.:format) confidential_memos#create
new_scenario_confidential_memo GET /scenarios/:scenario_id/confidential_memos/new(.:format) confidential_memos#new
edit_scenario_confidential_memo GET /scenarios/:scenario_id/confidential_memos/:id/edit(.:format) confidential_memos#edit
scenario_confidential_memo GET /scenarios/:scenario_id/confidential_memos/:id(.:format) confidential_memos#show
PATCH /scenarios/:scenario_id/confidential_memos/:id(.:format) confidential_memos#update
PUT /scenarios/:scenario_id/confidential_memos/:id(.:format) confidential_memos#update
DELETE /scenarios/:scenario_id/confidential_memos/:id(.:format) confidential_memos#destroy
scenarios GET /scenarios(.:format) scenarios#index
POST /scenarios(.:format) scenarios#create
new_scenario GET /scenarios/new(.:format) scenarios#new
edit_scenario GET /scenarios/:id/edit(.:format) scenarios#edit
scenario GET /scenarios/:id(.:format) scenarios#show
PATCH /scenarios/:id(.:format) scenarios#update
PUT /scenarios/:id(.:format) scenarios#update
DELETE /scenarios/:id(.:format) scenarios#destroy
Yet when I start my rails development server and visit http://localhost:3000/sims/scenarios/new , I get the error
Routing Error
No route matches [GET] "/sims/scenarios/new"
Why do I get this error when my path is in my rake routes?
The prefix sims is not present on the url I guess you meant. It would be http://localhost:3000/scenarios/new, ain't it?
Do you want to organize your scenarios under a sims namespace ? if so, check out routing in this guide:
http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing
In essence, you'd want to have something like:
namespace :sims do
resources :scenarios do
resources :confidential_memos
end
end

Why can't I access my Refinery CMS route?

I have the following routes in Refinery:
$ rake routes | grep blog
blog_root /blog(.:format) refinery/blog/posts#index
blog_post GET /blog/posts/:id(.:format) refinery/blog/posts#show
...
However, when I try to access that route, it gives an error.
> app.refinery.blog_post_path
ActionController::RoutingError: No route matches {:action=>"show", :controller=>"refinery/blog/posts"}
Here is part of routes.rb
# Refinery
mount Refinery::Core::Engine, at: '/'
Rails 3.2.14, Refinery 2.1.1.
Two things spring to mind
You probably don't need to preface refinery.blog_post_path with app although that doesn't seem to be the source of your error.
More importantly, you have not specified which blog post you want to show. So, your code should look something like refinery.blog_post_path my_blog_post.id

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.
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

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.

Rails, where is new_user_path? Don't see any reference to this path?

In this rails app:
https://github.com/ryanb/govsgo/blob/master/app/views/authentications/index.html.erb
There is a form that as 'new_user_path'
Where is the definition for this symbol? Is it a symbol??
Just confused as I downloaded the source, and searched for 'new_user' and couldn't find any reference to it.
It is located in routes.rb with the name new_user. It's not in this file.
EDIT:
routes.rb is here: https://github.com/ryanb/govsgo/blob/master/config/routes.rb.
But this is rails 3 format and I don't see new_user route. Run rake routes and you should see one called new_user.
I haven't downloaded the codebase but if you run rake routes you will probably see a route definition for new_user.
If you check config/routes.rb you will see the line resources :users. This creates several paths (like edit_user_path, users_path etc.), amongst them new_user_path.
You can see all generated path by running $ rake routes.

Resources