Using Formtastic to Cleanly Create Nice Looking Forms in Rails
Forms can easily get cluttered when you’re dealing with a lot of form fields… er, ERB tags. I’ve written about extending Rails form builders, which certainly goes along way to shrinking your views where forms are used. The plugin Formtastic is even better, as it lets you skirt maintaining your own library in favor of a very, elegant DSL.
For a great overview of the plugin and implementation details, check out Ryan Bate’s Railscast. There are a couple episodes, but be sure to catch the first one, linked above.
I usually install the plugin as a gem
# config/environment.rb #... config.gem "formtastic"
and then run a generator to create the stylesheets for me, making forms look nice and neat.
./script/generate formtastic
One gotcha, is that in order for the css to render correctly you need to add this
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
in the head of your layout file.
You define forms usingĀ “semantic_form_for” like so…
<% semantic_form_for @user do |f| %>
<% f.inputs do %>
<%= f.input :login, :label => "Username", :hint => "Something short because it's in the url"%>
<% end %>
<%= f.buttons %>
<% end %>
As per usual, there are a myriad of configuration options which can be overridden if necessary. Consult the documentation at Justin French’s Github account for specifics.
How Beautiful is Ruby?
Working with Ruby and in particular Rails, it’s easy to take the beauty inherent in the language for granted. I mean look at this code. If you read it aloud to yourself, it reads like an english sentence that any non programmer can understand.
Forum.categories.map do |category| link_to category.name, category end.to_sentence
Forum categories map do category, link to category name, the category, end and convert to a sentence. The code returns a an english sentence as well.
Fish, French Bread, Coffee and Hamburgers with each linked to the correct resource as well!
Active Record Find Methods
Active Record find methods for selecting range from http://charlesmaxwood.com/notes-from-reading-activerecordbase/
Student.find(:all, :conditions => { :grade => 9..12 })
return a range
Student.find(:all, :conditions => { :grade => [9,11,12] })
will return an "in()"
Hello Rack
What is Rack?
Rack provides a minimal, modular and adaptable interface for developing web applications in Ruby. By wrapping HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, web frameworks, and software in between (the so-called middleware) into a single method call.
- Rack API Docs
Create and name your file config.ru If rack isn’t installed get it with this command
gem install rack
In your config.ru file
require 'rubygems'
require 'rack'
class HelloRack
def call(env)
[200, {"Content-Type" => "text/html"}, "Hello Rack!"]
end
end
Rack::Handler::Mongrel.run HelloRack.new, :Port => 8888
Reading, Writing, Removing Files and Directories in Ruby
These aren’t all of them, but I think they are some of the most useful.
# making a directory in another directory that doesn't yet exist...
FileUtils.mkdir_p '/path/to/your/directory/that/doesnt/exist/yet'
# recursively remove a directory and the contents
FileUtils.rm_rf("/path/to/directory/you/want/to/delete")
# write a file from the contents of another file...
File.open("/path/to/the/file.ext", "wb") {|f| f.write(@your_other_file.read)}
Send Mail in Ruby with a Pony
Great little gem that let’s you quickly and easily send out mail from your ruby scripts.
gem install pony require 'rubygems' require 'pony' Pony.mail :from=>"me@example.com", :to=>"you@example.com", :subject=>"hello", :body=>"world"
Installing Monk on Ubuntu with Ruby Gems
Installing monk like this will fail
gem install monk
You’ll need to install the wycats-thor gem first with this command
gem install wycats-thor -s http://gems.github.com
Deploy Sintra App on Ubuntu Using Apache2 and Phusion Passenger Module
Check it out http://sinatra.seanbehan.com/
This assumes Apache2 and the Phusion Passenger module have already been installed. If not you can get up to speed w/ this resource http://seanbehan.com/ruby-on-rails/new-ubuntu-slice-apache-mysql-php-ruby-on-rails-git-and/
First you need Sinatra, so install the gem
gem install sinatra
We need a home for Frank, so create the minimum number of directories in our web directory. The public directory is where we’ll server images, stylesheets, javascript etc. The tmp directory will be where we control Passenger.
cd /var/www/sinatra mkdir myapp mkdir myapp/tmp mkdir myapp/public cd myapp vim index.rb #in index.rb get '/' do "Fly me to the moon..." end
Next we need a configuration file for Rack, important the file extension is “.ru” not .rb!
vim config.ru require 'rubygems' require 'sinatra' Sinatra::Application.default_options.merge!( :run => false, :env => ENV['RACK_ENV'] ) require 'index' run Sinatra.application
Set up the virtual host
<virtualHost *> ServerName www.myapp.com DocumentRoot /var/www/sinatra/public </virtualHost>
Now you can restart the app if you make adjustments!
touch tmp/restart.txt
Keep on singing :)
Inspired by http://blog.zerosum.org/2008/7/4/passenger-3-sinatra
Gem Information with Gem List
If you want to get version information about a gem the easiest way to do it is with this command
gem list <gem name>
For example
gem list activemerchant
will output the activemerchant versions I have installed. You can pass only part of the name like
gem list a
get the names of all gems (and versions) that start with “a”. There are a lot more options so here is a link to the gem command reference http://rubygems.org/read/chapter/10
The commands here are useful if you’ve installed a gem but the version you’ve configured in your environment.rb file is different. To quickly grab the right version, this command is pretty handy.


