Archive for Ruby

Sinatra Take #2

Because this will not be a ordered list of Sinatra posts…

I will mention right now some advantages of Sinatra

Use your toolbox!

  • You can choose your favorite template engine.
    • ERB, Haml, Builder, …

  • Do you have a favorite ORM?
    • ActiveRecord, DataMapper, Sequel, …

  • Javascript?
    • jQuery, Prototipe, Dojo, …

  • tests
    • Test::Unit, RSpec, Bacon, …

more to come…

Comments

Coding with Sinatra

As a SysAdmin I do love my work but sometimes we need to code also!

Some months ago I found this awesome microframework.
Sinatra





“… Sinatra is a DSL for quickly creating web applications in Ruby with minimal effort:…”

Well thats it. I need move some tasks from command line to the web and Sinatra is what I have found to be the most cool.


# sudo gem install sinatra
# vi mywebapp.rb

require 'rubygems'
require 'sinatra'

get '/' do
  erb :index
end

__END__

@@layout
<html>
  <head></head>
  <body>
    <%= yield %>
  </body>
</html>

@@index
Helloa!

# ruby mywebapp.rb ; lynx http://localhost:4567

Easy right? Next post I will introduce some more stuff about Sinatra and maybe CRUD operations coded from the root.

take care.

PS: I do need to think about this CSS!! It’s a PITA!!

Comments

Redmine 0.8 on Dreamhost

For those who are trying to get Redmine to work on Dreamhost using Passenger, check out these notes.I had lots of problems mostly because 1) of reading too much outdated information, 2) reading wrong information too, so… the simple way is:

Start from a clean base install…

 1. Cleaning up the house:
 1.1 Remove all stuff related with gems on you home directory
  $ cd ~ ; rm -rf .gem*
 1.2 Remove all GEM_PATH's in your .bash_profile (there are many howtos with inconsistent information about this)
  (after removing, reload it)
  $ source .bash_profile
 2. Install Rails 2.1.2 because Redmine 0.8 needs it
  $ gem install rails -v=2.1.2
 3. Check its installetion
  $ gem list --local | grep 'rails'
  rails (2.2.2, 2.1.2, 2.1.1, 2.1.0, 2.0.2)

Ok, now lets continue…

(quoting original post: Dreamhost howto)

  1. From the Dreamhost control panel, create a new subdomain for the application such as yoursubdomain.yourdomain.com
        1. Make sure the domain supports “Ruby on Rails Passenger (mod_rails)?”
        2. Specify your web directory: /home/username/yoursubdomain.yourdomain.com/public
               * you must add the public!!!
  2. From the Dreamhost control panel create a new MySQL database named yourdatabasename
  3. ssh into your Dreamhost account
  4. cd ~/yoursubdomain.yourdomain.com
  5. svn export --force svn://rubyforge.org/var/svn/redmine/branches/0.8-stable ./
         * check http://www.redmine.org/wiki/redmine/Download for the latest version
  6. also watch out for permissions
         * chmod -v -R 755 ./*
  7. cd ~/yoursubdomain.yourdomain.com/config
  8. cp database.yml.example database.yml
  9. nano database.yml
        1. edit the database.yml config file with the appropriate info. Should be similar to the following
           production:
           adapter: mysql
           database: yourdatabasename
           username: yourusername
           password: yourpassword
           host: mysql.yourdomain.com
 10. cd ~/yoursubdomain.yourdomain.com/public
 11. cp dispatch.rb.example dispatch.rb
 12. nano .htaccess (replace with following text)
     Options +FollowSymLinks +ExecCGI
     RewriteEngine On
     RewriteRule ^$ index.html [QSA]
     RewriteRule ^([^.]+)$ $1.html [QSA]
     RewriteCond %{REQUEST_FILENAME} !-f
     ErrorDocument 500 "H2Application errorH2 Rails application failed to start properly"
         * replace the H2 with the proper HTML tag when you place it in the .htaccess
 12. for now just remove .htaccess to get it working, then you can play with it at the end.
   $ mv public/.htaccess public/.htaccess_off
 13. cd ~/yoursubdomain.yourdomain.com
 13.1 Some chamges in environment.rb
   $ vi config/environment.rb
 13.2 Uncomment this line
   ENV['RAILS_ENV'] ||= 'production'
 13.3 and take attention to this one, we do want this version!
   RAILS_GEM_VERSION = '2.1.2' unless defined? RAILS_GEM_VERSION
 13.4 now lets point it to use our gem_path on our local gem directory
   ENV["GEM_PATH"]="/home/<YOUR USERNAME>/.gem:/usr/lib/ruby/gems/1.8"
  (Attention to .gem agains .gems directory from other tutorials.
    with a fresh clean install, rubygem install everything on .gem by default, so lets KISS)
 13.5 PStore error? Change this:
   #  config.action_controller.session_store = :P Store
      config.action_controller.session = {
            :session_key => "_myapp_session",
            :secret => "some secret phrase of at least 30 characters"
     }
 13.6 and finally freeze the app
   $ rake rails:freeze:edge TAG=rel_2-1-2
 14. from application root type
         * rake db:migrate RAILS_ENV="production"
 15. also type the following to load config defaults
         * rake redmine:load_default_data RAILS_ENV="production"
         * choose "en" for english
 16. Before pointint your browser to http://yoursubdomain.yourdomain.com
   you must RESTART your application. Some tutorials are wrong telling your to REBOOT the app.
   $ echo `date`>> tmp/restart.txt ; cat tmp/restart.txt
 16. browse to http://yoursubdomain.yourdomain.com

I hope this can help you! Drop me a post if you have some suggestions… I need them too :)

Comments (11)