Posted by Rob Anderton on April 21st, 2008 @ 10:48 – 0 comments
Updated on June 10th, 2008 @ 12:13
Tagged with validation
The default Rails behaviour for highlighting form fields with errors is to wrap them in a div, so:
<div>
<%= f.label(:email) %>
<%= f.text_field(:email, :size => '30') %>
</div>
Becomes:
<div>
<div class="fieldWithErrors"><label for="user_email">Email</label></div>
<div class="fieldWithErrors"><input id="user_email" name="user[email]" size="30" type="text" value="" /></div>
</div>
This is great when knocking together a quick prototype but as your site evolves you’ll probably want to customise it. This can be done very easily by using ActionView::Base.field_error_proc, assign a Proc to it either in your environment.rb or, if you’re a modern thinker, in an initializer and you’re good to go.
Read more of this entry
Posted by Rob Anderton on February 4th, 2008 @ 16:10 – 2 comments
Updated on June 10th, 2008 @ 12:15
Tagged with validation
Over the last year we’ve been involved in a wide variety of projects using Ruby on Rails and in that time we’ve had to use many different plugins. As you’d expect some have been better than others, so here’s a quick round-up of a few that I have found particularly useful.
Read more of this entry
Posted by Rob Anderton on December 16th, 2007 @ 19:34 – 3 comments
Updated on June 10th, 2008 @ 12:15
Tagged with validation
If you have a model that uses validates_acceptance_of, you have an observer for that model and you try to run the migration responsible for creating the underlying table then the db:migrate task will fail. The actual error you receive will depend upon your database engine – on MySQL it looks something like this:
Mysql::Error: Table 'database_name.table_name' doesn't exist: SHOW FIELDS FROM `table_name`
The culprit is changeset 8208 which allows the attribute used by validates_acceptance_of to actually exist in the database rather than being virtual. When running the migration to create the underlying table, environment.rb causes the model observer to be loaded which in turn loads the model itself. When the model is loaded validates_acceptance_of tries to determine the column names of the (as yet non-existent) table and, yes you guessed it, blows up!
The good news is that changeset 8377 was submitted 4 days ago to fix this problem. To take advantage of it you’ll either have to freeze Rails to the latest revision or you can simply apply the patch if you’re using something like Piston to manage vendor/rails.
18th December Update: Rails 2.0.2 has now been released and includes the fix for this problem.
Discuss this entry