Saturday, September 5, 2009

text_field_tag ... width/size

<%= text_field_tag :skip_to, @labour_start_day, :size => 8 %>

comparison of Date with ActiveSupport::TimeWithZone failed

Great article by Mark explaining what goes wrong.

http://marklunds.com/articles/one/402

Here is a quote:

With the timezone support introduced in Rails 2.1 the idea is that all dates in the database are stored in UTC and all dates in Ruby are in a local timezone. The local timezone can be specified by config.timezone in environment.rb or set to the user timezone with Time.zone= in a before filter. Typicaly, when reading/writing from/to the database ActiveRecord will transparently convert time attributes back and forth to UTC for you. However, there is a gotcha with datetimes in ActiveRecord::Base.find conditions. They will only be converted to UTC for you if they are ActiveSupport::TimeWithZone objects, not if they are Time objects. This means that you are fine if you use Time.zone.now, 1.days.ago, or Time.parse("2008-12-23").utc, but not if you use Time.now or Time.parse("2008-12-23")

Wednesday, September 2, 2009

Install Plugin from Git - Syntax

Here's the syntax to install a plugin from git.

./script/plugin install git://github.com/rails/open_id_authentication.git

Sunday, August 30, 2009

hidden field

<%= f.hidden_field :user_id, :value => current_user.id %>

or if you don't have f

<%= hidden_field_tag(:filter_only, :value => 1) %>

Default value on select tag

When using the Rails helper select_tag, the selected value (ie highlighted value) is a parameter to options_for_select, not select_tag. Eg:

Number of Rows: <%= select_tag(:numrows, options_for_select(%w{10 20 50 100 200 500}, session[:numrows])) %>

cited from: http://soniahamilton.wordpress.com/2009/03/02/ruby-on-rails-select_tag-default-value/

Saturday, August 29, 2009

Rails Validation

Here are the docs on rails validation:

http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M002164

What is the 'h' for in rails erb?

Often you see code like:

<%=h @person.name %>

The h doesn't seem to do anything if you take it out! The h is actually important, it is the html_escape. See the documentation:

http://api.rubyonrails.org/classes/ERB/Util.html

Here is a brief quote from the docs:

html_escape(s)

A utility method for escaping HTML tag characters. This method is also aliased as h.

In your ERb templates, use this method to escape any unsafe content. For example:

puts html_escape("is a > 0 & a < 10?")
# => is a > 0 & a < 10?