Steve
Steve

Reputation: 245

creating a controller in Rails

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

Upvotes: 0

Views: 3833

Answers (5)

raytiley
raytiley

Reputation: 684

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.

Upvotes: 2

jpsimons
jpsimons

Reputation: 28090

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 ]

Upvotes: 2

Bryan Ward
Bryan Ward

Reputation: 6693

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

Upvotes: 1

MattMcKnight
MattMcKnight

Reputation: 8290

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.

Upvotes: 0

Larry K
Larry K

Reputation: 49104

  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.

Upvotes: -1

Related Questions