Ruby on Rails index.html.erb not loading - ruby-on-rails

Just starting in Rails and I'm trying to get my index.html.erb page to show when I enter my rails-backend sub-domain.
I've been following the instructions from this guide but have run into some trouble. Searching google and SO for an answer did not yield results, so here I am.
I ran $ rails generate controller home index in order to generate index.html.erb, and deleted public/index.html as well. I then went to the routes.rb file in the /config directory and added this line:
root :to => 'home#index'
I also removed this line:
get "home/index"
My index.html.erb in the app/views/home/ directory looks like this:
<h1>Hello Rails, my best friend!</h1>
<p>Find me in app/views/home/index.html.erb</p>
<br />
<p>Also word to your mother</p>
I ran rake routes in the terminal and got this output:
(in var/www/testapp)
root / home#index
When I try to go to my sub-domain I get the following page:
I then tried uncommenting the get "home/index" line in routes.rb, but the same result showed up. Here is my home_controller.rb file, which I didn't touch but a) the guide didn't ask me to and b) looks normal compared to other controllers I've seen:
class HomeController < ApplicationController
def index
end
end
I'm sure this is a simple question, but I couldn't find any questions with a similar situation on SO. Thanks for helping!

Your code all looks correct. I would recommend foregoing Passenger and use the Thin server instead. It handles both http and https.
It's as simple as this:
Add the following line to your Gemfile: gem 'thin'
Install the gem: bundle
Start the server: bundle exec thin start --ssl
This way, you receive the benefits of the Mongrel parser and SSL without needing any custom Apache configuration.

Related

Rails Tutorial sample_app fails in Heroku with log: ActionController::RoutingError (No route matches [GET] "/about"):

I am following the online version of Rails Tutorial. The Sample_app in chapter 3 works fine locally, but when pushed to Heroku, the home page is found but not the other pages. Running heroku logs after trying to see the About page gives me (along with much else) the error above:
2015-08-09T02:56:43.916991+00:00 app[web.1]: ActionController::RoutingError (No route matches [GET] "/about"):
My routes.rb file is
Rails.application.routes.draw do
root 'static_pages#home'
get 'static_pages/help'
get 'static_pages/about'
end
I have followed the directions carefully. I tried deleting and recreating the Heroku file. I did web searches and tried some things to no avail. My gemfile is straight from the online version of the book, which is using current versions.
Solved: #thedanotto had me run heroku run rake routes which showed that the help and about pages were directed to {root}/static_pages/about rather than {root}/about. I am still puzzled why the tutorial gives the about routes, which appear not to work as expected, and welcome any further comment, but I am marking this as solved.
Whenever I can't find a route, I run the terminal command
rake routes
Since you're on heroku you'll want to run
heroku run rake routes
Which will return something similar to the following.
Prefix Verb URI Pattern Controller#Action
static_pages_about GET /static_pages/about(.:format) static_pages#about
So that shows that you can go to www.[heroku_app_name].herokuapp.com/static_pages/about And it will bring you to the page you want. You can also add a link to the page in a view by putting the following line of code within a view.
<%= link_to("About", static_pages_about_path) %>
That's all good stuff to know. But let's talk about using the controller action: static_pages#about with the path /about
Switch the following in routes.rb from
get 'static_pages/about'
to
get "static_pages/about", path:"/about"
or alternatively
get "/about", to: "rabbits#about"
You can read more about Routes here
If it works fine locally, I assume you've set up the controllers, views etc properly.
Have you made sure to commit all those necessary changes then push?
e.g.
git add .
git commit -am "Added route"
git push heroku master
Are you accessing the about page using the following URL ?
http://YourHerokuAppName.herokuapp.com/static_pages/about
[ Replace "YourHerokuAppName" with your Heroku App Name]

rails beginner, created 2 rails app on local dev

I am new to rails and I was able to install rails (3.2.3, ruby v1.9.3), then created a test app:
$ rails new Hello
then I cd'd into the new directory 'hello' and did the following commands:
$ rails generate controller home index
$ rails s
$ rake routes, it gave me
home_index GET /home/index(.:format) home#index
hello /hello(.:format) Hello#index
Then I pointed my browser to: http://localhost:3000/home/index - and it worked great.
Then I wanted to begin a tutorial and it asked me to create a new rails application
so I did like I did before...
$ rails new TutorialApp
$ rails generate controller tutorial index
$ rails s
$ rake routes, it gave me
tutorial_index GET /tutorial/index(.:format) tutorial#index
then I pointed my browser to: http://localhost:3000/tutorial/index, it gave me a message of
Routing Error
No route matches [GET] "/tutorial/index" Try running rake routes for
more information on available routes.
So I ran rake routes again, which it gave me the same output as it did before
tutorial_index GET /tutorial/index(.:format) tutorial#index
Since I created that first rails app "Hello", do I need to turn off that app before starting the new rails app "Tutorial" or they can both be running at the same time?
Any help is appreciated, thanks!
You can use a different port
rails server -p 3001
It will run in a different port. Then, just point to http://localhost:3001
But usually you will probably stop the server on one app and start the other one. It's up to you.
When you run rails server ("rails s"), you're typically running it in the context of the rails project you're in at the time, so before beginning a new project, I would shut down the current server (CTRL C). Also, make sure you create a new rails app in a folder is isn't itself at the root of a rails app. It looks like you might have created your tutorial app inside the root of your Hello app. Sounds like those are the two main things that tripped you up.

Resque-web path not working

I just upgraded to rails 3.1 and is getting a weird routing problem with resque-web
I've mounted resque-web on my routing.rb like this:
mount Resque::Server, :at => "/resque"
when my server is up going to 0.0.0.0:5000/resque works fine but declaring the path 'resque_path' in my erb.html files gives me
undefined local variable or method `resque_path' for #<#<Class:0x000001038e9eb0>:0x000001038a7650>
it's even listed on my rake routes as:
resque_server /resque {:to=>Resque::Server}
'resque_path' should be working! i hav no idea why it is now. anyone?
If you check rake routes you can see that solution is:
resque_web_path
Have you tried resque_server_path? That could do it.

Nesta CMS and Rails3 integration: adding blog to an exiting rails 3 application

I'm adding nesta (0.9.8) cms, to an existing Rails 3.0.10 application. I get the blog up and running but not the layout/stylesheets.
What i did until now is :
1. inside rails app main root, add gem 'nesta', gem 'sass' and run
'bundle'
2. run "nesta new nesta-blog"
3. edit config.ru like following :
require ::File.expand_path('../config/environment', __FILE__)
map "/" do
run MyRails3App::Application
end
require 'nesta/env'
require 'nesta/app'
Nesta::App.root = ::File.expand_path('./nesta-blog', ::File.dirname(__FILE__))
map "/blog" do
run Nesta::App
end
4. edit config/routes.rb like following :
require 'nesta/env'
require 'nesta/app'
Rails3MongoidOmniauthSimple::Application.routes.draw do
mount Nesta::App.new => "/blog"
root :to => "home#index"
...
5. cd nesta-blog
6. run nesta demo:content
Now, if you run rails s from your ~/main-rails-app, going to http://localhost:3000/blog you will see the the demo nesta site but without his default layout/stylesheets, while if you run shotgun config.ru from inside ~/main-rails-app/nesta-blog, going to http://localhost:9393/ everything shows up correctly.
Any suggestion?
Thanks in advance
Luca G. Soave
I've not got this to the level of plug-n-play that I'd like yet, but I'm running Nesta on my Rails 3.0 sites by adding this to config/routes.rb:
mount Nesta::App, :at => '/'
match '/css/*style.css' => Nesta::App
match '/attachments/*file' => Nesta::App
I haven't looked into a cleaner way of doing this yet (i.e. avoiding having to specify css and attachments routes as well).
I created my Nesta app in a directory located at "#{Rails.root}/nesta". I also needed an in config/initializers/nesta.rb:
require "nesta/env"
Nesta::Env.root = ::File.expand_path("../../nesta",
File.dirname(__FILE__))
I quite like the way you've done it too.

Rails on remote Apache server not displaying index.html.erb

I played around with Rails on my laptop (running Linux + Apache + MySQL) and had no trouble getting the Getting Started with Rails tutorial to work locally. Now I'm trying the same thing at work on a remote Mac OS X + Apache server, and things aren't quite so rosy.
I typed rails blog -d mysql to create a directory called blog in /Library/WebServer/Documents/mydirectory. The trouble is, if I go to server.com/mydirectory/public, I get the public/index.html in my browser. But, I don't get this file if I go to server.com/mydirectory/. Instead, I get a 403 error. Also, when I:
script/generate controller home index
to create:
app/views/home/index.html.erb
I am unable to view this file, whether I go to server.com/mydirectory/home/index, or if I add a new line (map.root :controller => "home") to config/routes.rb and go to server.com/mydirectory.
Am I missing something really obvious about Apache and Rails?
Apache does not support Rails out of the box. You have to get mod_rails aka Passenger installed. Or, you could just use the server that comes with Rails, which is much easier (but not suitable for production). To do this, go to your directory and do ./script/server.

Resources