Rails: root :to => "controller#method" - ruby-on-rails

A few questions about this route:
root :to => "controller#method"
When a GET request is made in what part of Rails does it equate GET "/" with root?
What is root (i.e. a variable, method, other)?

root as well as match define verb-independent routes, meaning all http verbs are included in the routing rule. If you want to define verb-dependant rules, you can use get/post/put/delete rules like:
get 'profile', to: 'users#show'
In particular, root defines what the default page (when you acess the root path of your application) is.
You can find all the information you need here:
http://guides.rubyonrails.org/routing.html

Related

How to remove a specific route from a mounted engine in rails 4

In my routes.rb file I have mounted engine routes.
mount SomeEngine::Engine => '/', as: 'some_engine'
It adds this route:
match '*path' => 'redirects#show',
constraints: SomeEngine::RedirectConstraint.new,
via: 'get'
What this route does is that it catches all unknown URLs and passes them to SomeEngine::RedirectConstraint
I don't need that kind of functionality. Is it possible to remove that route from main app? I don't have an access to engine. Actually I need to add some routes after that engine and now they can't be reached because of match '*path' route.
In Rails routes: The priority is based upon order of creation: first created -> highest priority. This may help you.

Rails Routes with Rapidfire

I'm working on creating an app that allows users to create surveys. I decided to add in the rapidfire gem which really helps to speed up the process of making surveys.
So far it's working great, however the "Home" link in my head that points to the root_url no longer brings me back to the root, but the "rapidfire" homepage.
Here's my config/routes.rb file:
SurveyMe::Application.routes.draw do
devise_for :developers
devise_for :users
mount Rapidfire::Engine => "/surveys"
root "static_pages#home"
match "/about", to: "static_pages#use", via: "get"
match "/developers", to: "static_pages#developer" , via: "get"
match "/how", to: "static_pages#how", via: "get"
when I do rake routes I get this:
rapidfire /surveys Rapidfire::Engine
root GET / static_pages#home
about GET /about(.:format) static_pages#use
developers GET /developers(.:format) static_pages#developer
how GET /how(.:format) static_pages#how
Routes for Rapidfire::Engine:
results_question_group GET /question_groups/:id/results(.:format) rapidfire/question_groups#results
question_group_questions GET /question_groups/:question_group_id/questions(.:format) rapidfire/questions#index
POST /question_groups/:question_group_id/questions(.:format) rapidfire/questions#create
new_question_group_question GET /question_groups/:question_group_id/questions/new(.:format) rapidfire/questions#new
edit_question_group_question GET /question_groups/:question_group_id/questions/:id/edit(.:format) rapidfire/questions#edit
question_group_question GET /question_groups/:question_group_id/questions/:id(.:format) rapidfire/questions#show
PATCH /question_groups/:question_group_id/questions/:id(.:format) rapidfire/questions#update
PUT /question_groups/:question_group_id/questions/:id(.:format) rapidfire/questions#update
DELETE /question_groups/:question_group_id/questions/:id(.:format) rapidfire/questions#destroy
question_group_answer_groups POST /question_groups/:question_group_id/answer_groups(.:format) rapidfire/answer_groups#create
new_question_group_answer_group GET /question_groups/:question_group_id/answer_groups/new(.:format) rapidfire/answer_groups#new
question_groups GET /question_groups(.:format) rapidfire/question_groups#index
POST /question_groups(.:format) rapidfire/question_groups#create
new_question_group GET /question_groups/new(.:format) rapidfire/question_groups#new
edit_question_group GET /question_groups/:id/edit(.:format) rapidfire/question_groups#edit
question_group GET /question_groups/:id(.:format) rapidfire/question_groups#show
PATCH /question_groups/:id(.:format) rapidfire/question_groups#update
PUT /question_groups/:id(.:format) rapidfire/question_groups#update
DELETE /question_groups/:id(.:format) rapidfire/question_groups#destroy
root GET / rapidfire/question_groups#index
I guess I don't really understanding what the "mount Rapidfire::Engine => "/surveys"" line is doing. Is there anyway I can adjust the root that it's generating so that it indeed points back to my original app's home page?
RapidFire, as an engine, has it's own root which is mounted under the /surveys path that you specified. Calling root_path from within a RapidFire namespaced controller/view will refer to the that root - not the sitewide root.
To fix this, you can pass the sitewide root path to the engine, and call that from the header instead - see Rails.root from engine for a way to do this.

Custom url in ruby on rails

I know rails uses the controller action style urls like www.myapp.com/home/index for example
I would like to have a url like this on my rails app, www.myapp.com/my_page_here is this possible and if so how would I go about this?
You just use a get outside of any resources or namespace block in your routes.rb file:
get 'my_page_here ', :to => 'home#index'
Assuming you are using Rails 3+, do NOT use match. It can be dangerous, because if a page accepts data from a form, it should take POST requests. match would allow GET requests on an action with side-effects - which is NOT good.
Always use get, put, post or these variants where possible.
To get a path helper, try:
get 'my_page_here ', :to => 'home#index', :as => :my_page
That way, in your views, my_page_path will equal http://{domain}/my_page_here
you just need to make a routing rule to match that url
in this case it will be something like
match 'my_page_here' => 'your_controller#your_action'
your controller and action will specify the behavior of that page
so you could do
match 'my_page_here' => 'home#index'
or
get 'my_page_here', :to => 'home#index'
as suggested in other responses.
for index action in home controller if you have such a controller
see http://guides.rubyonrails.org/routing.html for more details
also see Ruby on Rails Routes - difference between get and match

Rails Routing (root :to => ...)

I know how to set the routes root of my rails app to a controller and an action.
But how to add an id?
/pages/show/1 should be the root.
How do I set this?
Had this same problem and this worked for me:
root :to => "pages#show", :id => '1'
As of Rails 4.0, you can declare the root route like this:
root 'controller#action'
I'm using Rails 5.1 to point the home page to a specific blog. In config/routes.rb I have ...
root 'blogs#show', {id: 1}
This will point the root route to /blogs/1
I'm doing this on a blog site I'm building. The first blog will be the main site blog as well as the homepage.
Cheers
Matthew's solution works, but I think it is more readable to fetch the object. For example, let's say you want to root to the Page#show action for the page with the name "landing". This is a bit more readable:
root :to => "pages#show", :id => Page.find_by_name("landing").id
From a performance perspective, this solution is worse because it requires an additional database query, but this solution is more readable if performance is not a high priority.
Try:
match 'pages/show/:id' => 'pages#show', :as => :root
In Rails console. rake routes | grep root, should show something like:
root /pages/show/:id(.:format) {:controller=>"pages", :action=>"show"}
Hope that helps.
Use Rails 5.1
Add this to the config/routes.rb
root 'pages#show', {id: 1}

The :as attribute in my root line in config/routes.rb is messing up my application. What's wrong with it?

I have a model and controller called coves_controller. I have this line in my routes.rb file right now: root :to => 'coves#index', :as => 'coves'
When I comment it out and go to localhost:3000/coves, everything works fine. When I uncomment it, I'm unable to create a new cove object.. There is no error statement, it just doesn't add to the database.
When I change 'coves' to 'cove' at the end, I'm can create new cove objects, but after doing so I'm routed to coves/.5 which should be coves/5
If you look at how the root path is defined, it's just a convenience method which creates a named route for you. The reverse_merge method used to load in the options means that your :as => 'coves' part of the hash will be getting ignored and changed back to :as => :root. You cannot rename the root path when using the root method.
As #apneadiving said, you need to use resources :coves to get RESTful CRUD routes for it.
I guess there is a conflict with the paths created by:
resources :coves
That is basic REST behaviour.
Don't add a 'as' statement for you root, root is just root :)

Resources