简体   繁体   中英

Default not getting selected in Formtastic form

I'm working in Rails 2, and my model look like this:

class Site < ActiveRecord::Base
 has_many :contacts

 def primary_contact
  Contact.find :first, :conditions => {:site_id => self, 
                                       :primary_contact => true}
 end

 def primary_contact= new_primary_contact
  contact = Contact.find(new_primary_contact)
  contact.primary_contact = true
  contact.save
 end
end

Note that the:primary_contact virtual attribute is initialized in the model, so the default value should be present.

The form for @site in my view contains this input:

<%= form.input :primary_contact, :as => :check_boxes, :collection => (Contact.find(:all, :conditions => {:site_id => @site.id}, :order => "first_name ASC")), :label => false, :include_blank => false %>

This gives me a list of checkboxes, with the correct default checkbox already checked (ie the primary contact for that site is selected). However, I want this in the form of radio buttons or a select drop-down, and in those two cases, just by changing :as =>:check_boxes to :as =>:select or :as =>:radio , the default isn't selected.

I also tried specifying the collection as just :collection => @site.contacts , but same thing.

Any idea what I'm doing wrong?

Which version of Formtastic? Sounds like it could be a bug. If you can still repeat after upgrading to Formtastic 1.2.4, please file an issue on Github.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM