On Nov 13, 11:55 pm, "Mislav Marohnić" <[EMAIL PROTECTED]>
wrote:
> I'm puzzled by this latest commit:
> "Tag helper should output an attribute with the value 'false' instead of
> omitting the attribute, if the associated option is false but not 
> nil."http://github.com/rails/rails/commit/4e9abdd7f1b4e05f8d1b50ddaa080b3f...
>
> What was the reason for this? How does it help in HTML? Thanks

It's because Rails 2.2 changed the way empty strings are mapped to
boolean model attributes.

Migration:
  create_table :people do |t|
    t.boolean :is_alien, :null => false
  end

Rails 2.1:
  p = Person.new(:is_alien => "")
  p.is_alien   # => false
  p.save       # => true

Rails 2.2:
  p = Person.new(:is_alien => "")
  p.is_alien   # => nil
  p.save       # => StatementInvalid: value for is_alien may not be
null in INSERT statement!


In my controller I have this:
  def new
    @person = Person.new
    @person.is_alien = false
  end

  def create
    @person = Person.new(params[:person])
    @person.save
  end

In the 'new' view:
  <% form_for @person do |f| %>
    <%= f.hidden_field :is_alien %>

Before this commit, the hidden_field helper would output:
  <input type="hidden" name="person[is_alien]" />
As you can see, there's no "value" attribute. Upon submitting the
form, @person.is_alien would be set to nil instead of the expected
false, causing an SQL error. This is solved by making hidden_field
output value="false":
  <input type="hidden" name="person[is_alien]" value="false" />
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to