Hey folks i am having some issues with rails root routs. For some reason I can not get root url (localhost:3000/) to route to the appropriate place.
I have built a brand new app and used scaffold to generate the "questions" model. I can confirm that the"index" action exists (by default from scaffold)
Here is my code:
Fbauth::Application.routes.draw do
resources :questions
root :to => 'questions#index'
end
Rake Routes Output:
(in /home/jsfour/rails3_apps/fbauth)
questions GET /questions(.:format) {:action=>"index", :controller=>"questions"}
POST /questions(.:format) {:action=>"create", :controller=>"questions"}
new_question GET /questions/new(.:format) {:action=>"new", :controller=>"questions"}
edit_question GET /questions/:id/edit(.:format) {:action=>"edit", :controller=>"questions"}
question GET /questions/:id(.:format) {:action=>"show", :controller=>"questions"}
PUT /questions/:id(.:format) {:action=>"update", :controller=>"questions"}
DELETE /questions/:id(.:format) {:action=>"destroy", :controller=>"questions"}
root /(.:format) {:controller=>"questions", :action=>"index"}
What is the problem here? Why is localhost:3000/ giving me the "welcome to rails" message?
You can specify what Rails should route '/' to with the root method: You should put the root route at the top of the file, because it is the most popular route and should be matched first. The root route only routes GET requests to the action. You can also use root inside namespaces and scopes as well. For example:
2 Resource Routing: the Rails Default. Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. Instead of declaring separate routes for your index, show, new, edit, create, update and destroy actions, a resourceful route declares them in a single line of code.
Each method is a request to perform an operation on the resource. A resource route maps a number of related requests to actions in a single controller. When your Rails application receives an incoming request for: it asks the router to map it to a controller action. If the first matching route is:
Browsers request pages from Rails by making a request for a URL using a specific HTTP method, such as GET, POST, PATCH, PUT and DELETE. Each method is a request to perform an operation on the resource. A resource route maps a number of related requests to actions in a single controller.
You need to delete public/index.html - it will have priority over your root action.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With