// Internet Duct Tape

Getting Started With Ruby on Rails – Week 2

Posted in Ruby on Rails, Technology by engtech on November 28, 2007

Learning Ruby

I’ve fallen for the hype and started using Ruby on Rails for building database driven web applications. You can follow along with my weekly experience discovering gotchas with Ruby on Rails.

Previously: Getting Started With Ruby on Rails – Week 1

Emacs Rails-mode

Last week I complained about wasting time setting up rails-mode in emacs. I’m starting to find some real time saver though. The navigation short-cuts are absolutely necessary for navigating the file structure of a Rails application and I really like how the syntax highlighting can capture lines that don’t make any sense to Ruby. This is a great feature if you’re learning Ruby at the same time as you’re learning Rails. It has auto completion for “”, [], {} and ending function blocks and even picks up things like when you have one too many ends in your file.

Which files should be checked in?

I couldn’t find a list of what files are allowed to be checked in anywhere in Agile Web Development with Rails. The answer seems to be anything but:

db/schema.rb # easier to let your db:migrate control it
config/database.yml # because it contains database passwords
coverage/* # generated by rcov
logs/* # generated by server
tmp/* # temporary sessions files

Don’t overwrite the flash

Bad, no validation errors will be shown:

@model.save
flash.now[:notice] = "I saved it"

Better:

if (@model.save) then
flash.now[:notice] = "I saved it"
end

Will trap and display ruby errors as well as validation errors:

begin
if (@model.save) then
flash.now[:notice] = "I saved it"
else
  raise "Error saving"
end
# Do stuff
rescue Exception => e
  flash[:notice] = e.message
end

Keep controllers streamlined

I found myself creating one controller that had add/show/delete/list actions for multiple models. It’s much cleaner to have multiple controllers for the individual models.

Conditional Linking

link_to_if will put an unlinked version of the text if the conditional is false. This is much more useful than removing the link text completely for a lot of situations, because you don’t have to worry that the rest of the text around it will look weird. Don’t try to use html_options as a hash! I lost quite a bit of time to this because it won’t use the method parameter, but it doesn’t give you an error.

link_to_if (check_if_user_can_delete),
	"Delete Image",
	{ :action => "delete", :id => @image.id },
	:confirm => "Are you sure?",
	:class => "dangerous",
	:method => :delete

Generate validates_* off of database

I would have liked it if the generate script automatically generated validates_* helpers off of the database table. validates_length_of could be generated for :limit and validates_numericality_of could be generated for :integer.

Using the same partial to display create/edit/show

This is a neat little trick I found. You can use the same partial for your create/edit/show actions by using html_options and setting

{ :disabled => (controller.action_name == "show" ? true : false) }

for all the fields. It might not be useful for many public applications, but for an internal app it’s a great way to use the same ajaxy displays that you use for create/edit in show.

Polymorphic Associations

Are weird if you want to validate uniqueness. They might work better with has_many relationships than with has_one relationships. Or, I made it more complicated that it had to be.

Deploying sucks

It’s true. Played around a bit with capitrano and vlad the deployer but the both seem to assume you’re using subversion.

Free Tidbit: How crypt works in passwd files

I don’t know why this was so hard to find in Google: passwd files that use the crappy crypt mechanism use the first two characters of the expected password as the salt!

given_password = "hello_world"
encrypted_password = "ahga3sgj"
return encrypted_password == given_password.crypt(encrypted_password.slice(0,2))

and don’t worry, I’ll talk about something other than Rails later this week :)

5 Responses

Subscribe to comments with RSS.

  1. Jason King said, on November 28, 2007 at 9:05 pm

    Instead of doing the if(@model.save) then blah; else throw “…”; end take a look at @model.save!

  2. engtech said, on November 29, 2007 at 12:38 pm

    turns out checking uniqueness of polymorphic associations is easier than I thought.

    validates_uniqueness_of :resource_id, :scope => :resource_type

  3. Ryan Davis said, on November 29, 2007 at 4:41 pm

    Uh, no. Neither vlad nor cap assume you’re using subversion. They default to it but both are very good at letting you use whatever you want.

    Maybe you should actually look at them.

  4. [...] Skip navigation Subscription OptionsMost Popular PostsIDT Labs – Free Software ToolsWordPress.com Resources – Tips, Tricks and ToolsWordpress.com Theme ReviewsWordpress.com Theme Review HelpGreasemonkey script: WordPress Category ResizerWordpress.com 7 Day Referrer ParserPerl Script – WordPress.com 7 Day Referrer ParserGreasemonkey Script: Akismet Auntie Spam for WordpressGreasemonkey Script: Find images that are too wideTag Cloud Generator for Wordpress.comTag Cloud Generator AdvancedTag Cloud Generator – Release NotesWordPress Themes by InternetDuctTape.comBlack and Blue and Read All Over Theme for WordPress SandboxMoon Under Uranus Theme for WordPress SandboxMiscellanious WordPress Scripts and ToolsGreaseMonkey Script: WordPress Comment NinjaTechnorati Favorite Your FansTechnorati Favorite Your Fans – Release NotesComic BloggerGreasemonkey script: Flickr always search for Creative Commons licensed photosGreasemonkey Script: Yahoo Pipe CleanerTag CloudAll Posts by Category and TitleSeriesGift Guide for Geekseaster eggsReader Appreciation for RSS subscribersWelcome to Internet Duct Tapegoogle1ec000b3808eedbf.htmlAbout MeDisclosureImages Credits « Getting Started With Ruby on Rails – Week 2 [...]

  5. [...] Previously: Getting Started With Ruby on Rails – Week 2 [...]


Comments are closed.

Follow

Get every new post delivered to your Inbox.

Join 287 other followers

%d bloggers like this: