views:

175

answers:

5

I'm trying to use the generate script to create a controller. I run the following command:

> ruby script/generate controller Greeting

and the controller seems to be generated no problem. Then I add a method called index to the GreetingController:

class GreetingController < ApplicationController
  def index
    render :text => "<h1>Welcome to your first Rails application<h1>"
  end
end

I then start the WEBrick web server, and direct my browser to http://127.0.0.1:3000/greeting, but I get an error message in the browser saying:

We're sorry, but something went wrong.

We've been notified about this issue and we'll take a look at it shortly.

It should be working, at least according to the book I'm reading, Ruby on Rails by O'Reilly. Any idea what could be going wrong? The book was written a few years back, and I'm using what's probably a newer version or Rails. Any ideas?

UPDATE Here's what's in development.log:

/!\ FAILSAFE /!\  Sat Nov 28 22:11:12 -0500 2009
  Status: 500 Internal Server Error
  no such file to load -- mysql
    /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:156:in `require'

FYI I created the application by calling:

rails /home/myuser/www/mynewapp -d mysql
A: 
  1. Check the log in your /log directory. It will give you far better error information.
  2. My guess is that you're getting a double render error--since you're calling render explicitly, you need to put a return statement after your render call. -- By default, the controller tries to render the view that matches your controller after the controller's method completes.
Larry K
adding return doesn't seem to help. I posted the first few lines of the development.log file. I can post the rest if need be, but I suspect that first error message is the problem. I'm going to look into it...
Steve
I tried just what he had and it worked fine. It's not a double render error.
MattMcKnight
This error would occur before the View is rendered.
Joseph Silvashy
A: 

It's trying to load MySQL driver. Did you edit config/database.yml? Mine looks like this:

# SQLite version 3.x
# gem install sqlite3-ruby (not necessary on OS X Leopard)
development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

Also, switch to Agile Web Development with Rails, Third Edition. It's a good tutorial and still mostly works with the latest version of rails, even though it is written for 2.2.

MattMcKnight
thanks for the book recommendation! I will see if they've got it on Safari
Steve
+1  A: 

From the brief error you have posted you might want to check that you have the mysql gem installed if you are planning on using mysql.

If you are on linux try:

gem install mysql
Bryan Ward
yes! and also just to make sure have it in your PATH Variable in your shell profile as well.. that way you can test it.
Joseph Silvashy
+2  A: 

You're not able to load the MySQL database driver. I'm guessing it's not installed. You could try this:

sudo gem install mysql

I'm guessing though that you probably don't have a MySQL database set up. Most people use sqlite3 for development. Your config/database.yml should look like:

development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

If you don't have sqlite3 installed just run

sudo gem install sqlite3-ruby

Then run your migrations and you should be all set

rake db:migrate

Update I posted this here so I could use some formatting.

In the future just call:

rails /path/to/app

This will make Rails use the default database which is sqlite3. You can then change the production database if/when you decided to deploy.

Good luck.

raytiley
yup, you pretty much got it totally covered here.
Joseph Silvashy
Hey Steve,What OS are you running. Your obviously missing something :) I've seen errors like that on OS X system where I've forgotten to install developer tools. I would run a google search along the lines of <strong>Your operation System</strong> install sqlite3
raytiley
Oops didn't realize that html tags weren't allowed in comments. Ignore the <strong> tag :)
raytiley
I'm running Ubuntu. I solved the problem. I needed to call "sudo apt-get install sqlite3 swig libsqlite3-ruby libsqlite3-dev" before I could install that sqlite3-ruby gem. Thanks for the help! I am now up and running with Rails!!! :-)
Steve
+2  A: 

Rails assumes you'll be using a database. If you don't give it a valid connection string in database.yml, it chokes right out of the gate. This is a valid assumption since any real web app will be using a database.

But, if you're just trying to mess around with how the views and controllers work, you can disable the database functionality entirely. To do so, add this to your config/environment.rb:

config.frameworks -= [ :active_record ]
darkporter
That's a great debugging tip!
Jay Godse