How to set the value of a string field using a checkbox? - ruby-on-rails

In a Rails app I have a model with a string attribute, that can only accept a short list of possible values.
A gender select is a good example of this:
(using simpleform)
<%= f.input :gender, :collection => [["male"],["female"]], :prompt => "Select" %>
and provides three states, 'male', 'female', and unselected.
In an effort to improve UX, we're replacing this select field with two checkboxes with values of 'male' and 'female', which again provides three options due to some jquery magic.
male / false
false / female
false / false
The problem is, the value of the first checkbox is alway overwritten by the value of the second on form submit.
Now I could do something with jquery on the client side to deal with this, I could also create a method on the server to deal with this. But that is not the purpose of this question.
I've never been confident that I fully understand all of the various features and uses of f.check_box, check_box object and check_box_tag. Is there a clever way to achieve the desired result within a Rails form, or am I on the wrong track?

Related

Rails Simple Form :first :last

Writing a fairly long form using the simple form gem but having trouble with the radio button collections. Users are asked questions and have radio buttons to choose from but the value to be entered is different than what is being displayed for the user. Here is an example line of my code,
<%= f.collection_radio_buttons :shower_flow_rate, prompt: "Do you have low flow shower heads?",[[2.5, 'Yes'] ,[3.8, 'Some'], [5.0, 'No']], :first, :last%>
:shower_flow_rate is the variable I want filled. What are :first and :last? Are they necessary? I found very little documentation explaining the :first and :last.
Also I have several attributes in the same schema that need to be filled by calculating the sum or product of some of these inputs in the form. Should these values be calculated in the view or in the controller?
In you case yes they are necessary, first & last are array methods. (You can use any methods instead of :first, :last which is valid for the each element of the collection)
Ref collection_radio_buttons
In you case following is collection
[[2.5, 'Yes'] ,[3.8, 'Some'], [5.0, 'No']]
So, :first & :last are the methods which will call on each
element of the collection to create an HTML
# For Ex:-
# [2.5, 'Yes'].first => 2.5
# [2.5, 'Yes'].last => 'Yes'

Sipmle Form For - displaying a value different from the model attribute

I have a form where I wish to perform a calculation on a date before displaying it. The model has a field for start, and a method adjusted_start and adjusted_finish which I want to display instead of the raw attribute.
I've tried:
f.input :start, as: time, label: false, minute_step: 5, value: openingtime.adjuted_start
However this is over ridden with the raw 'start' attribute. Is there any way to get it to display a different value than the one it has stored for start?
Thanks in advance.
I guess it should be input_html: { value: openingtime.adjuted_start }.
You need to wrap html attributes in input_html hash.
Check out doc.

Active admin: Custom check_box input

I need to show a dynamic list of values to show on an Active Admin edit screen as checkboxes, where the list comes from code (not database). I can do that pretty easily, but I can't figure out how to show some of those as being checked.
Here's a simplified example of what I'm trying to do:
names = %w(Sam Darcy Ernie)
pairs = Hash[names.zip(names)]
f.input :buddies, as: :check_boxes, collection: pairs, checked: %w(Sam)
What I was hoping for is to show the 3 checkboxes and have the "Sam" box checked. None are checked though. What can I do to control which checkboxes are checked?
I ended up with the following that functions as desired:
people = [
['Sam', 0, checked: true],
['Darcy', 0],
['Ernie', 0],
]
f.input :buddies, as: :check_boxes, collection: people
It seems that the array items beyond the first 2 are used to set attributes. So in my case, the "checked" attribute is set, resulting in the element attribute of checked="checked"
I'm still interested in knowing if there is a better way of handling this though.

Rails - Tagging with has_many through and select2

I'm implementing select2 tagging with a has_many through relationship. My implementation has 2 different scenarios.
The select menu allows multiple (tagging) but does not allow on the fly input into the select menu.
Same as above but uses ajax to allow the user to enter new select values on the fly.
Scenario 1 works well for tagging. Scenario 2 seems to work (can perform tagging) but does not save the values. My problem seems to come down to my my input elements for the scenarios.
Scenario 1 uses:
<%= f.association :repairers, label_method: :rep_name, value_method: :id, include_blank: true, label: 'Repairer'%>
and when the form is submitted gives params similar to:
"repairer_ids"=>["", "1132", "1131"]
Scenario 2 uses:
<%= f.hidden_field :repair_type_id, :class => "required on-the-fly-select select"%>
and uses a lot of js code to implement on the fly input for the select menu. When the form is submitted the data will look like:
"repair_type_id"=>"5688,5690"
So with scenario 2 the ids are not submitted as an array. I have tried changing the select to:
<%= hidden_field_tag("repair_item[repair_type_ids][]", "", :id => "repair_item_repair_type_ids", :class => "required on-the-fly-select select") %>
but then the relevant param is submitted incorrectly and doesn't save:
"repair_type_ids"=>["5688,5690"]
Is it possible to get the params from Scenario 2 to submit in the format that Rails made the params in Scenario 1?
Try String#split
repair_type_id = "5688,5690"
repair_type_id.split(',')
=> ["5688", "5690"]
It's Better to use this split method in controller while saving the data on particular attribute for this instead of on hidden_tag.
Edit:
If you want to convert string in plural then use pluralize same as for singularize
2.0.0-p481 :010 > 'cat'.pluralize
=> "cats"
2.0.0-p481 :011 > 'dogs'.singularize
=> "dog"

observ_field with collection_select on rails 2.3.8?

I'm developing project on rails 2.3.8 and I need to observe field on drop down menu which develop using collection select. Please can some one explain how to observe field ?
My collection select code is like this
<%= collection_select("event", "trainer_id", #trainers , :id, :name, {:prompt => true}) %>
And I don't know how to use observe field for this. So please can some one explain about this ?
Related: Auto populate a text field based on another text field
observe_field(field_id, options = {})
Observes the field with the DOM ID specified by field_id and calls a callback when its contents have changed. The default callback is an Ajax call. By default the value of the observed field is sent as a parameter with the Ajax call.
Read this details: http://apidock.com/rails/ActionView/Helpers/PrototypeHelper/observe_field

Resources