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.
Related
I have snippet:
<%= f.input :purpose, as: :radio_buttons, collection: category.subcategories,
wrapper: :vertical_collection_inline %>
which lines values of category.subcategories horizontally how I want
The problem is, when I select either of option, it assigns that option's ID, but not its value.
How should I refactor the code?
Using IDs has advantages as warned in the comments, however what you’re trying to do should work with either:
category.subcategories.collect(:&values)
Where values is the name of the field which hols “Rent” etc.
The more railsy way to do this is with collection_radio_buttons, like this:
f.collection_radio_buttons(:purpose, category.subcategories, :value, :value)
Again where “value” is the field name.
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'
I have a search field where I want to add two dropdowns that should help me filter my search results. In my database I have one Product model with two columns, named country and type.
The first dropdown should filter by country and the second should filter by type. However, I am trying to set the second dropdown dynamically meaning that depending on the first dropdown (country) only those types should be displayed in the second dropdown that match the respective country selected. If possible this behavior should also work the other way around i.e. depending on the type, only matching countries should be displayed.
I've been trying several tutorials that work with more than one model however, I didn't manage to get it to work with only one model. My code for the dropdown selections is the following:
<%= form_tag("filter", :id => "filter_form", :method => "post") do %>
<label for="country" class="country">Country</label> <%= select_tag :country, options_for_select(Product.pluck(:country).uniq), { include_blank: 'Select country' }%>
<label for="type" class="type">Type</label>
<%= select_tag :type, options_for_select(Product.pluck(:type).uniq),{ include_blank: "Alle Kategorien" }%>
Is is possible with only one model?
Do I need AJAX for this or is Javascrip/jQuery sufficient?
Thank you very much!
I am very new to Ruby on Rails, I have inherited control of a Ruby/Rails web based database(created by another) for generating and tracking engineering functions. I use Aptana IDE.
My issue is that I am trying to use a form helper to select multiple values for our "product lines". I use 10 check boxes, and I get the correct output of one value if I don't use ":multiple => true".
See output image:
works as intended
<%= f.check_box :product_line, {:class => "field"}, "A9", false %> A9
(10 times with different values where "A9" is, so there are ten checkbox's total, image shows "A7" check box returned)
By simply adding the :multiple => true, the output changes as seen in the image below:
see output image:
returns too much
<%= f.check_box :product_line, {:class => "field", :multiple => true}, "A9", false %> A9
Why are all of those "-" added before the output only if the multiple selection is made? I want it to return only comma separated values of the checkbox, i.e. " A9, A7"
PRODUCT_LINES = [ "A9", "A7", "AG", "AF", "S3", "Legacy", "K/Kpro", "EMW", "HD", "Non-Metallic" ]
other ideas:
Can I loop through an array of :product_line to get what im looking for instead?
I was also playing with the multiple selection in a drop down menu but could never select a second option before the drop down returned and only selected my first selection.
Why are all of those "-" added before the output only if multiple
selection is made?
From the docs (read the Gotcha) when multiple is set to true all selections will be stored in an array of product_lines but un-selected check_boxes will also be reserve as an empty strings in the sent pararms so for the result you are seeing product_lines would be something like:
product_lines = ["", "", "", "A9", "A7"]
Can I loop through an array of :product_line to get what im looking
for instead?
Well, actually this takes us right to the problem in using multiple: true in your code.
The process explained in (1) above is how the check_box_tag was designed, the problem you are seeing is in presentation probably in your ECN#show. Without reviewing code there i am only guessing but usually it's something like:
<%= #ecn.product_lines.join('-') %>
which should be
<%= #ecn.product_lines.reject(&:blank?).join(',') %>
where: reject(&:blank?) to get rid of empty strings then joining with commas as you need
Hope this helps!
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?