Image with Checkbox instead of text - ruby-on-rails

I have an association where a Vote has many Images, and I'd like this association to be checkboxes but instead of text have an image next to the checkbox instead of a text value the an example of the outputted HTML would look like:
<input class="check_boxes optional" id="vote_image_id_1" name="vote[image_id][]" type="checkbox" value="1"><img src="boat.png" alt="Big Boat"> <br />
Is this possible to do with simple form or will I need to write a custom field for this? If i have to write a custom field could someone recommend some good resources on making custom fields with simple form.

I can't test it right now, but I would do it more or less like this:
options = []
images.each do |image|
options << [image.id, image_tag(image.path)]
end
f.collection_check_boxes :votes, options, :first, :last
Maybe you can extract the construction of the options array to a helper method.
I hope it helps :)

Related

Is possible to add a namespace to the inputs in a form divided by partials?

I'm using simple form and I have a long form divided in partials, each partial represents a category.
For example, identification_form is:
I'd like the inputs to get a name like this:
Is possible to do this?
I can change the names by myself, but the problem is simple_form is not able to create the names for the stat_date and end_date (act[start_date(1i), act[start_date(2i), act[start_date(3i), it adds the same name for the three inputs.
Thanks!
Yes it is!
f.fields_for :identification do |identification_form|
identification_form.input :name
The above will give you:
<input type="text" name="act[identification][name]" />
This should then work with dates.
Edit:
I think you should be using date_select rather than input.

how to put place holder for <%= f.datagrid_filter filter%>?

I made a rails application, and I used datagrid gem to handle filters, pagination,and orders(ascending and descending). I was supposed to write <%= f.datagrid_filter filter%> to filter according to a filed of the table(Ex:title field in topics table).
Now <%= f.datagrid_filter filter%> returns a traditional html input tag like below. <input id="topic_report_title" class="title string_filter" type="text" size="30" name="topic_report[title]"> in the html console.
Now I want to put placeholder in that helper method only.
Can anybody help please?
Have you tried to do the following: <%= f.datagrid_filter filter, :placeholder => "placeholder text"%>

Creating a form with unknown fields and storing those fields into a serialized filed in my model

I have a form that will have both a dynamic set and a known set of fields. I need a way of storing the dynamic fields in the database and I have decided on storing them in a serialized field, as I will not need to search on the data, and I just need it stored and recalled when needed.
class MyApplication < ActiveRecord::Base
has_one :applicant
belongs_to :member
serialize :additional_fields, Hash
accepts_nested_attributes_for :applicant, :additional_fields
I was thinking of having the form return the fields as an additional_fields_attributes and somehow have the model look after storying the hash into the additional_fields section. Im not sure if I have to go as far as using something like method missing to look after this, or if I should scrap the accepts_nested_attributes_for and handle it on my own.
Does anyone have any thoughts?
Thanks! Ryan
I just tested what you suggest.
You don't need: accepts_nested_attributes_for :additional_fields
Just add in your form html like:
<input name="my_application[additional_fields][first]" type="text" />
<input name="my_application[additional_fields][second]" type="text" />
it will save a Hash with keys: first and second
You could put in your model an array of fields, say in your User model:
FIELDS= ["item1", "item2"]
In your view:
<% User::FIELDS.each do |field|%>
<input name="my_application[additional_fields][<%= field %>]" type="text" />
<% end %>
I ended up using this tutorial http://www.kalzumeus.com/2009/11/17/practical-metaprogramming-with-ruby-storing-preferences/ which worked really well.
Thanks for your help!

Replace 'collection_select, :multiple => true' with multiple 'check_box' options in Rails

How do you replace a collection_select (with :multiple => true) with a list of check_box options, such that there is a check_box option for each object in the collection?
Is there an elegant way to implement this using form builders (i.e. without using *_tag helpers)? I'd like to lean on ActiveRecord's built in functionality as much as possible...
I don't think there's a built-in "elegant" way to do this.
This railscast should get you going, though:
http://railscasts.com/episodes/17-habtm-checkboxes
http://asciicasts.com/episodes/17-habtm-checkboxes (text versionn)
You can do it like this (example using HAML).
%fieldset
Colors I like
- %w[red blue].each do |color|
= f.label color
= f.check_box :colors_liked, {multiple: true}, color, nil
The nil option at the end prevents Rails from building a hidden checkbox by the same name with a 0 value, which you definitely don't want if you're going for multiple selection.
This produces:
<label for="colors_liked_red">Red</label>
<input id="my_form_colors_liked_red" \
name="my_form[colors_liked][]" type="checkbox" value="red">
<label for="colors_liked_blue">Blue</label>
<input id="my_form_colors_liked_blue" \
name="my_form[colors_liked][]" type="checkbox" value="blue">
When the form is submitted, the parameters will contain an array of the values of the checked options.

rails fields_for with ajax

I am building a dynamic form builder.. And i have a problem which i can't seem to fix.
So i have a db table called "forms"
forms can have "fields"..
The problem is that when a user creates a new 'field' (click add-field) then it should ajax the new field for .. that field.
The problem is that i can't just do something like this:
<%= Form.fields_for Field.new do |field| %>
<%= field.text_field :name%>
<% end %>
Does anybody have an idea? Yes i watch railscasts, yes i googled, yes i found the "complex-forms' repo on github.
But no luck (yet)
If you want an all javascript approach (instead of calling your server to produce the field names) then basically you just need to increment the field names for any new fields.
For example, if you have
class Form < ActiveRecord::Base
has_many :fields
accepts_nested_attributes_for :fields
and the HTML in the form has an input field that has something like
<label for="form_fields_attributes_0_name">
<input id="form_fields_attributes_0_name" name="form[fields_attributes][0][name]" type="text" />
then you need to write some javascript to make it look like
<label for="form_fields_attributes_1_name">
<input id="form_fields_attributes_1_name" name="form[fields_attributes][1][name" type="text" />
You can do something like
$('#form_fields_attributes_1_name').attr('id').split('_');
and
$('#form_fields_attributes_1_name').attr('name').split(/\]\[/);
to get at those numbers.
Here's an example which is refactored here.

Resources