It used to be that routes.rb had a root route line. But I don't see it anymore even though when I run the server I see the "Yay! You’re on Rails!" page. I can't find where it is defined so I can override it!
On a new project it doesn't use a root route, it just renders the rails' default new project page.
Just add the line:
root to: 'somecontroller#someaction'
and you are done.
The routes.rb file is in the config/ directory of your rails project. You can define a root route there and direct it using the routes DSL
Above answers are correct but incomplete.
With Rails 6 it seems that there's no explicit root route ('/'). Instead the rails gem(s) handle it by displaying the standard "Yay you're on Rails" page (see railsties/templates/welcome/index.html.erb). It's a fixed page, bypassing routes.rb and layouts etc.
This behavior seems to only happen in development mode and when you haven't actually defined a root route.
So it seems that the page is fixed and unmodifiable. But it can easily be replaced by a root route.
Related
I have just begun using rails so I apologize if this is a dumb question, but I have been googling for a few hours and I'm starting to get really frustrated.
I've set up an AWS Bitnami Rails stack using the free one on the AWS Marketplace, and I've been following the tutorial at http://guides.rubyonrails.org/getting_started.html to start getting my head around running an app functionally. (I've already done rails for zombies and ruby 101)
Basically, I get to section 4.3 which is supposed to set me up with the basic routing for the app and I can't seem to get it to work. No matter what I put in the routing file of the welcome_controller, the app just serves the template index file located at /opt/bitnami/ruby/lib/ruby/gems/2.3.0/gems/railties-4.2.5.1/lib/rails/templates/rails/welcome/index.html.erb
My routes.rb file is
Rails.application.routes.draw do
get 'welcome/index'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'welcome#index'
and my welcome_controller is
class WelcomeController < ApplicationController
def index
end
end
I really hope someone can help.
I'm guessing this is some sort of really stupid config error but I have a feeling others might have it as well so maybe this could be of use in the future.
Two possibilities:
You didn't restart the rails server after you generated controller. Though, /welcome/index etc should still work without restart.
Your server is running from directory which doesn't have this code checked in. As your error log doesn't reports absence of routes altogether.
I have created my apps and everything is there but when I go to my host address, I cannot see anything, it gives me 404 NOT FOUND error. I also did rails server but it seems to hang. It looks like it just stops at WEBrick:HTTPSERVER#start: pid=3457 port 3000. And yes, when I ran rails server it was under the app folder.
I also went to port 3000, it says this page is not available.
When I add an index.html file, I can see that file. But this is obviously not what I want.
What else do I need to check to get to my app?
You need to configure the default route for your application, also known as root.
Add a line like this to your routes.rb file:
root to: 'newpage.html'
Or, if you want to route to a controller, like the show method on the People controller:
root to: 'people#show'
Here is the Rails guide on routing
Try to run the app in the new port as:
rails s -p 4000
Place root :to => "static#index" in your routes file and add an index file in public/.
I'm running Rails 2.3.4. When I create a new rails project, the public/index.html file has a link named "About your application's environment" that points to "rails/info/properties". In dev mode, it gives a summary of the runtime environment. However, in production mode, it gives a 404 page cannot be found.
Could someone point me in the direction of how and where the "rails/info/properties" route is configured? I'd just like to understand how it's set up.
The link fires off an AJAX request to rails/info/properties. The properties action is defined in the Rails::InfoController which lives in /rails/railties/builtin/rails_info/rails/info_controller.rb.
The route doesn't need to be explicitly defined because it conforms to the Rails default route of :controller/:action/:id (although there is no ID in this case and the controller lives within a Rails namespace.)
It's configured within Rails itself (when in development mode). You can probably track it down if you look through the Rails initialization code.
Did the rules for Rails Routes changes from Rails 1.2.3 to Rails 2.2.3? I recently upgraded an application and instead of redirecting to the correct page, it now redirects to the main page (or Route page) for that matter.
The only thing that I can think of is that the routing rules changed in Rails 2.2.3.
Thanks
Yep there were a lot of changes as you can see here (look at the Action Pack Resources section) "This is where the bulk of the action for 2.0 has gone". If you're using the old semi-colon syntax then that could well be the problem. Do you want to post your route config and a quick description of what is not working?
Also, to diagnose routing problems I find running "rake routes" very useful (though set your terminal window to wide).
Chris
I have apache 2.2 with mod_rails running at http://localhost. I want to have my rails app at http://localhost/railsBlog. So, what I did was, I created a virtual host:
ServerName localhost
DocumentRoot /Library/WebServer/Documents
RailsEnv development
RailsBaseURI /railsBlog
Now, since the URL is http://localhost/railsBlog, the server views railsBlog as the controller I'm passing in, which is not what I want. So when I go to http://localhost/railsBlog/home/index. This won't get to my 'home' controller and 'index' view because it tries to go to 'railsBlog' controller (doesn't exist) and 'home' view (doesn't exist).
I think one way to solve this is to redefine map.root to be /railsBlog and things should be fine. But how?
Another way I could get around this would be to modify config/routes.rb to have:
map.connect 'railsBlog/:controller/:action/:id'
However, this would mean that I would have to change this file every time I deploy to a different location.
Or, is there any other way to get around this?
You can put a line like this in config/environment.rb (or one of the specific environment files)
config.action_controller.relative_url_root = "/railsBlog"
You should also symlink the publc directory to the root of the web directory, for example:
ln -s /rails/railsBlog/public /webroot/
This is all from the passenger documentation
Just to add to the previous answer... here is the url of the documentation:
http://www.modrails.com/documentation/Users%20guide.html#deploying_rails_to_sub_uri
Also, here is some information if you are running into errors with broken image, css, resource links...
http://www.modrails.com/documentation/Users%20guide.html#sub_uri_deployment_uri_fix
Basically it says you should always use the rails helper functions (image_tag, javascript_include_tag, and stylesheet_link_tag) instead of hand coding the urls. These will automatically generate the correct URL with the sub uri you have set.
This allows you to easily move the application to another sub uri or out of the sub uri configuration without changing all of your references.
One nice thing about this is that you can use one virtual server statement to deploy mutiple applications by having multiple RailsBaseURI lines. This came in handy for an app we were trying to build.