rubyTrials

A way to keep track of breakthroughs I've made in Ruby and Ruby on Rails.

My Photo
Name:
Location: Cadillac, Michigan, United States

Sunday, October 15, 2006

Free


Free
Originally uploaded by ObedientMuse.
You shall be free indeed when your days are not without a care nor your nights without a want and a grief,... - Kahlil Gibran

Thursday, September 07, 2006

substruct

I've recently discovered substruct. Just wanted to announce that I will be using it as the base for several new projects.

Try it out: http://dev.subimage.com/projects/substruct

Friday, July 21, 2006

check box tag

Here's a quicky when dealing with the check_box_tag.

syntax:
check_box_tag name, value, checked

code:
<%= check_box_tag :is_hourly, 1, :checked %>

outputs:
<input checked="checked" id="is_hourly" name="is_hourly" type="checkbox" value="1" />

Sunday, May 14, 2006

conditional layouts

I don't know about you, but I always make a admin tool for all my rails projects. Being generally lazy I don't like making new controllers just for the admin, but I do make a different layout. How then to get the controller to display the right layout based on which layout the response came from?

I found the easiest way was to pass a

:layout => "admin"

parameter in the routes for each action I wanted to apply the admin layout to. That's what routes are for right?

Anyways, to make this magic happen it takes code in two separate spots.

* In the controller in question do:

layout :get_layout


* In the application.rb (controller) do:

private
def get_layout
if params[:layout]
return params[:layout]
else
return "application"
end
end


In fact if you need this for all controllers just throw the

layout :get_layout

right in the application.rb too. I love inheritance! Don't you?

Now you have the ability to do conditional layouts to your hearts content!

Sunday, April 30, 2006

rails // migrations or migrains?

Tired of using mysql clients? Click ... id, click ... int, click ... 11, (client crash!!) and so on? Why not just do all of your db creation, modification, and removal using ruby in one simple file?

That's what migrations are for. Here's a quick step through* on how to get migrations working in rails for the first timer:

  1. Open terminal
  2. Go to your rails project directory
  3. script/generate migration migration_name
  4. Open up your friendly editor (hint: textmate) and edit the db/migrate/001_migration_name.rb file: if you are using Text Mate you can simply type "mate db/migrate/001_migration_name.rb" to pull it up.


In the migration file you can do cool things like creating tables, removing tables, adding columns, and the like. We're going to create a new table.


class InitDbTables < ActiveRecord::Migration
def self.up
create_table :blogs do |table|
table.column :title, :string
table.column :description, :text
table.column :user_rating, :integer, :default => 0
table.column :active, :boolean, :default => true
table.column :created_by, :integer
table.column :created_on, :datetime
table.column :updated_on, :datetime
end

def self.down
drop_table :blogs
end
end


Great! Now what? Well now we want to create an actual database:

In terminal: mysql -u username -p -e "create database blog;"

Now just edit your database.yml to reflect the proper database, password, et cetera.

Next edit config/environment.rb and change this line (on line 39 for most basic installs):

# config.active_record.schema_format = :ruby

To this

config.active_record.schema_format = :ruby

Finally, back at the terminal again, type: rake migration
Watch the magic unfold...

If you don't like what it did you can always undo it. Just go back to your friendly terminal and type: rake migration VERSION=0

This allows for proper versioning. You can rake migration to any version you have. Anytime you need to add to your db or table, or remove for that matter, just make another migration. Eventually you'll find your self becoming more proficient with your db design and you'll find less versioning needed. But until that day, try this and throw your sql clients in the trash where they belong.

Hack away!

*This assumes that you are using OS X. If you are using windows simple replace terminal with command prompt.

Monday, March 20, 2006

ruby on rails // current_page?

I recently ran into a situation where I needed to know what the current page was in order to apply the proper css class in a view in rails. [That's a long sentence ;)] Anyways it turns out to be cake. Snippet:


<ul>
<% unless current_page? :action => "index" %>
<li><%= link_to "Home", {:action => "index"} %></li>
<% else %>
<li class="tab_menu_active">Home</li>
<% end %>
</ul>


Also, in case anyone wondered, this is code from a divless/tableless layout. I do all of my sites this way now.

Friday, December 09, 2005

If Short Circuit Reminder

Here's a often forgotten shortcut for simple if's:
variable=condition ? value_if_true : value_if_not_true

Very useful for quick things in Rails controllers.

Scaffolding Fallout

Here's a great article about the abuse and final sadness of counting on scaffolding for your Rails work. I'm so glad to be past that stage. Luckily I came to Rails after doing my own Ruby work: in-fact I was starting to create my own web framework before finding Rails as some of you know. Read the article. It's very good.