JRuby/Rails

After several failed attempts at getting jruby/rails to work nicely with sqlite3, I fell back to mysql w/ this tutorial: http://blog.emptyway.com/2008/04/08/120-seconds-guide-to-jruby-on-rails/

After reading that, I am trying to blend those ideas with the tutorial located here.

Generate the rails project, using mysql as the database.
jruby -S rails -d mysql

Edit the config/database.yml file to point at the jdbcmysql driver
development:
adapter: jdbcmysql  # was mysql
encoding: utf8
database: blog_development
pool: 5
username: root    # use some actual username
password:               # use a password!
socket: /tmp/mysql.sock

Create the database
jruby -S rake db:create

Generate the scaffold for a Post
jruby ./script/generate scaffold Post title:string body:text

Run the db/migrations
jruby -S rake db:migrate

Start the server
jruby ./script/server

Go to the website
http://localhost:3000/posts

Add some validations to the app/models/Post.rb
class Post < ActiveRecord::Base
validates_presence_of :title, :body
end

Allow comments to be associated with posts
jruby ./script/generate scaffold comment post_id:integer body:text

jruby -S rake db:migrate

Edit Post.rb:
has_many :comments

Edit Comment.rb:
belongs_to :post

Edit config/routes.rb
map.resources :posts, :has_many => :comments     # posts/:post_id/comments/:comment_id

Reminder: use jruby -S rake routes to view how resource urls are mapped.

Then make necessary changes to comments/_comment.html.erb and posts/show.html.erb.
You will also need to make changes to the app/controllers/comments_controller.rb

~ by bandwagonblog on January 13, 2009.

Leave a Reply