I have a form I'm trying to set up ...
Users can have many posts, and each post can have many people watching it.
The Watch model is set up polymorphically as 'watchable' so it can apply to different types of models. It has a user_id, watchable_id, watchable_type and timestamps as attributes/fields.
This is soley so that when people comment on a post, users watching the post can get an email about it.
What I'm trying to do is show the user a list of users that they can tag on each post, which is not problem. This is what I'm using right now
http://pastie.org/940421
<% semantic_form_for #update do |f| %>
<%= f.input :subject, :input_html => { :class => 'short' } %>
<%= f.input :site, :include_blank => false, :input_html => { :class => 'short' } %>
<label>Tag Users (they will get notified of this update)</label>
<%= f.input :user, :as => :check_boxes, :label => ' ', :wrapper_html => { :class => "radiolist clearfix" }, :for => :watch, :name => "Watch" %>
<%= f.input :note, :label => 'Update'%>
<% f.buttons do %>
<%= f.commit_button :button_html => { :class => 'submit' } %>
<% end %>
<% end %>
The problem with this, is that when you go to edit an update/post ... all the checkboxes are prechecked ... I want it to pre-check only users who are currently watching the post.
To further clarify ... this is the hacky way I'm getting it to do what I want right now
<ul class="radiolist clearfix">
<% #users.each do |user| %>
<li>
<%= check_box_tag 'user_ids[]', user.id, #watches.include?(user.id) ? true : false -%>
<%= h user.name -%>
</li>
<% end %>
</ul>
where #watches is just an array of user ids
#watches = #update.watches.map{|watcher| watcher.user_id}
For anyone else having the same issue:
<%= f.input :some_input, :as => :boolean, :input_html => { :checked => 'checked' } %>
For multiple check boxes, this way:
<%= f.input :tags, :as => :check_boxes, :collection => some_map.collect { |c| [c[:name], c[:id], {:checked=> tag_ids.include?(c[:id])}] } %>
If you need the state of the checkbox to reflect the value of :some_input
<%= form.input :some_input, :as => :boolean, :input_html => { :checked => :some_input? } %>
In your model..
def some_input?
self.some_input ? true : false
end
Set the boolean attribute's value to 'true' in the controller before your render the form. That should make Formtastic default the checkbox to 'checked'.
Related
I have a Rails app that uses the same events/_form.erb for new and editing an event.
If it's a new event, I want to pre-select today's date. If it's being edited, I want to have it show the already entered date.
I thought I could do this with a flash setting.
This is the events/new.html.erb:
<% flash[:newevent] = "true" %>
<%= render :partial => 'form' %>
This is the code in events/_form.html.erb
<% if flash[:newevent] = "true" %>
<%= f.input :starts_at, :as => :string, :label => 'Date', :input_html => {:class => 'datepicker', :value => Date.today.to_s} %>
<% else %>
<%= f.input :starts_at, :as => :string, :label => 'Date', :input_html => {:class => 'datepicker'} %>
<% end %>
Thanks for the help!
Is this you're looking for ?
... :value => f.object.starts_as || Date.today.to_s
controller:
#new_event = true # or false in edit
view:
<% if #new_event %>
or just this in in view:
<% if f.object.new_record? %>
I can't get an image to show in a Formtastic form with radio buttons. Here's my form. The :activity_id, :as => radio is the issue:
<%= semantic_form_for #event do |f| %>
<%= f.inputs do %>
<%= f.input :date, :as => :string, :input_html => { :class => 'jquery-ui-date'} %>
<%= f.input :date, :as => :hidden, :input_html => { :id => 'date-alt'} %>
<%= f.input :activity_id, :as => :radio, :collection => Activity.all %>
<% end %>
<%= f.actions do %>
<%= f.action :submit, :as => :button %>
<%= f.action :cancel, :as => :link %>
<% end %>
<% end %>
That works fine as far as showing the radio button, the activity name, and saving correctly on submit. But I need to show the image that goes with the activity because most of my users cannot read and must rely on images and TTS. I've got the images showing in my other views.
I've tried dozens of combinations of things with :hint, and :wrapper_html, and image_tag and so on. So many variations I've become scrambled in the brain-pan.
A sample attempt:
<%= f.input :activity_id, :as => :radio, :collection => Activity.all, :hint => f.template.image_tag(f.object.image_url(:thumb)) %>
This gives me an "undefined method `image_url'" error. Yet this :hint works fine elsewhere.
I'll switch back to a standard html.erb view if that helps, but my problem there was a "stringify_keys" error I was unable to solve.
There's gotta be a way. Please? Thanks much. . .
I've got a nested form (using Ryan B's nested_form gem) using a has_and_belongs_to_many to has_and_belongs_to_many setup:
Opening has_and_belongs_to_many :contacts
Contact has_and_belongs_to_many :openings
When trying to add a new contact to an opening, in this case I get:
Can't mass-assign protected attributes: new_1346666966632
for
"opening"=>{"contacts_attributes"=>{"new_1346666966632"=>{"contacts"=>{"name"=>"Test Contact",
I've added the corresponding "accepts_nested_attributes_for" and "attr_accessible", and am building the contact i.e. #opening.contacts.build and #opening.contacts.build(params[:opening][:contact_attributes]) in the controller.
Where am I going wrong? Would it be better to use a has_many through relationship here?
EDIT:
View:
<%= simple_nested_form_for #opening, :wrapper => :plain do |f| %>
<%= f.link_to_add "Add a contact", :contacts %>
<%= f.button :submit %>
<% end %>
Which uses a partial to generate fields for nested contact:
<%= f.fields_for :contacts, #opening.contacts.build do |contact_form| %>
<%= contact_form.input :name, :label => false, :input_html => { :class => 'span6' } %>
<%= contact_form.input :company, :label => false, :input_html => { :class => 'span6' } %>
<%= contact_form.input :telephone, :label => false, :input_html => { :class => 'span6' } %>
<%= contact_form.input :email_address, :label => false, :input_html => { :class => 'spa12' } %>
<% end %>
You need to be building/creating the contacts from the opening model, as opposed to trying to assign the contacts_attributes manually. Your controller code needs to look something like:
#opening.update_attributes(params[:opening])
Check out the Rails guide for more info on using nested attributes
I want to make chain selects part inside my Formtastic form. But is it possible to set custom ids for multiple selects for future AJAX replacement?
This doesn't work:
<%= semantic_form_for [:admin, #production_year] do |f| %>
<%= f.inputs do %>
<%= f.input :car_model, :label => "CarModel", :as => :select, :collection => Brand.find(:all, :order => "name ASC"), :id => "brand_id" %>
<% end %>
<% end %>
Options should be passed to the input_html hash, like so:
<%= f.input :car_model, :input_html => { :id => "brand_id" } %>
EDIT: looking for this: http://diminishing.org/extending-formtastic-with-a-sprinkle-of-jquery (If this works I'll answer my own question)
I've started to create an in and out type habtm (has many and belongs to many) form through formtastic. However I would like it to be more flexible to the user, meaning if you select a name on one side you either click a button that moves it right away (ajax on click) or after the form is submitted
(I'm using formtastic with this, but it doesn't have to in anyones examples)
What I am struggling with is the javascript, in and out buttons..
Comparison Model
class Comparison < ActiveRecord::Base
has_and_belongs_to_many :devices
end
Devices Model
class Device < ActiveRecord::Base
has_and_belongs_to_many :comparisons
end
Comparison Controller
def edit
#comparison = Comparison.find(params[:id])
#devices = Device.find(:all, :select => 'id, device_name', :order => 'device_name')
#devices_selected = #comparison.devices.find(:all, :order => 'device_name', :conditions => ["id IN (?)", #devices])
#devices_not_selected = Device.find(:all, :order => 'device_name', :conditions => ["id NOT IN (?)", #devices_selected])
end
Comparison Edit View
<% semantic_form_for #comparison do |f| %>
<% f.inputs do %>
<%= f.input :comparison_name %>
<%= f.input :description %>
<h3>
Select Devices
</h3>
<% f.inputs :class => 'inline_fields' do %>
<%= f.input :devices,
:collection => #devices_not_selected,
:label_method => :device_name,
:label => 'Add Devices',
:input_html => { :size => 20 },
:include_blank => false,
:hint => 'Select devices from this list to compare to the parent device' %>
<%= f.input :devices,
:collection => #devices_selected,
:label_method => :device_name,
:label => 'Remove Devices',
:input_html => { :size => 20 },
:include_blank => false,
:hint => 'Deselect devices from this list to take them off the comparison' %>
<% end %>
<% end %>
<%= f.buttons %>
<% end %>
I'll be using jquery to fix this problem:
http://quasipartikel.at/2009/05/10/jqueryui-multiselect/
or
http://blog.jeremymartin.name/2008/02/easy-multi-select-transfer-with-jquery.html#