Getting Started With Ruby on Rails – Week 2

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 comments