Monthly Archives for April 2007

12
APR

This is my first ever published screencast. I’m pleased to introduce a quick guide to saving your Rails sessions in a database and a quick introduction to using sessions in Rails.

This is aimed at the Rails Beginner.

View Now

Firstly, I need to apologise for some of the poor sound quality. I will work on producing higher quality audio next time.

The software used in the screencast is: e-texteditor (beta), Navicat, Firefox and PuTTY SSH Client.


07
APR

When I first started using Rails I was puzzled why when submitting a form as an object, any field with errors where enclosed inside a <div> rather than an inline tag such as <span>. If you add <div>s around your field this may affect your website’s output by pushing anything next to your field onto a new line (outside the new DIV block).

Now, there are two ways you can resolve this issue.

1) You can add a CSS selector for #fieldWithErrors to set the display type to ‘inline’:

#fieldWithErrors { display:inline; }

2) As this is Rails, we can easily just change the markup which is output. We do this by modifying the ActionView::Base controller in our config/environment.rb file. Simply add the line below to this file to change the markup to use <span> tags insted of <div>.

ActionView::Base.field_error_proc = Proc.new {|html_tag,instance|%(<span class="fieldWithErrors">#{html_tag}</span>)}

Personally, I recommend the second option.


05
APR

Migrations in Rails

Posted by Adam Posted in Programming 0 comments

This post is follow on from a recent post of mine on RailsForums and it outlines some of the slightly more advanced methods of using migrations in rails.

Foreign Keys

You can’t directly set foriegn keys with migrations (yet), although you can add a SQL command to a migration for FK using:

execute "alter table table_name add constraint fk_name_of_fk /
foreign key (field_name) references foriegn_table(id)<

There is a plugin which handles this fully if you need it. Here.

Primary Keys

Primary Keys can be set using:

create_table :table_name, :primary_key => "field_name"

You simply need to add the primary_key to the create_table call. Rails by default will add an id field as the primary key. This option overrides that primary key.

If you change your primary key don’t forget to tell your model about the new one using self.primary_key = “whatever"

NOT NULL

t.column :field_name, :type, :null => false

This should be specified in your model using validates_presence_of as well

Unique

Unique should really be handed within your model rather than at a SQL level – using validates_uniqueness_of inside the model.


01
APR

Powered by Rails?

Posted by Adam Posted in 2 comments

I’ve just made a few Powered by Rails icons, planning on making a few more in the next few days but these will do for now.