I have a following ActiveAdmin form
form do |f|
f.inputs do
f.input :dropdown_a, # Dropdown menu
f.input :dropdown_b, # Dropdown menu
f.input :quantity
f.input :remarks
end
end
I want to dynamically select options of dropdown_b depending on value of dropdown_a. How can I achieve this? It doesn't have to be javascript. I am fine with reloading page with selected values onchange.
I guess you could extract something from this question:
Rails - pass a parameter to controller from form select dropdown
Using as collection for the second dropdown a variable, nil at the start, and once the user change the first dropdown an ajax request is made and the variable receive the new value from params.
Related
I'm using simple_form to submit a form with multiple fields and some dropdown values. When the form is rendered with errors, all text fields are remembered, but some of the dropdown values are resetted. All values are permitted in strong params, and pass validate_presence_of validation.
The collections are created in my model using class methods. As follows:
def self.options
['One','Two','Three']
end
And loaded in my form using:
<%= f.input :dropdown, collection: MyModel.options, include_blank: false %>
What should I do correctly render the form object when it is returned with errors?
f.select :dropdown, MyModel::myoptions, {include_blank: false}
And the method should be like this
def self.myoptions
[["One","One"],["Two","Two"],["Three","Three"]]
end
Please try and let me know.
I use simple_form and have this in my form code:
<%= f.input :fruit, as: :check_boxes, collection: ['Apple', 'Orange', 'Other'] %>
So I want to have a text input associated with 'Other' field next to it, so that user can click 'Other' and enter his own fruit name. How do I do it?
I tried it like f.input(...) as one of collection items, I've also tried adding it in do block, but it all seems like totally irrelevant.
How do I get the behavior I want? Thank you.
You can do this:
# view
<%= text_field_tag :alternative_fruit %>
# controller
if params[:alternative_fruit].present? && params[:your_model_name][:fruit].select(&:present?).blank?
params[:your_model_name][:fruit] = params[:alternative_fruit]
end
This will overwrite the fruit name if you didn't select a checkbox with the alternative name, if you defined one.
So I have a CareerEntry model that has a fullintern attribute, which is a string that is supposed to specify whether the entry represents an internship or a full-time position. I limit the values that can appear in this attribute as follows:
validates_inclusion_of :fullintern, :in => ["Internship", "Full-time"]
However, in ActiveAdmin, the part in the edit form that deals with the fullintern attribute still has a text field. How do I make it a dropdown box where the admin can select either "Internship" or "Full-time"?
Thanks.
You can use Formtastic's input helpers to use a select input:
form do |f|
f.inputs "Details" do
f.input :fullintern, as: :select, collection: ["Internship", "Full-time"]
end
f.actions
end
See Formtastic's usage section for the full set of native capabilities.
I have the code for a dropdown menu in a form using haml
= f.select :people, options_for_select(['Kid','Youth','Man','woman'])
After form submit the value is stored in db. Now in Edit form I want this dropdown menu with the value selected according to stored value in database. How to do it using haml
= f.select :people, options_for_select([your options here], selected: #variable.people)
where #variable is the name of the object you're getting the option for.
When creating a standard Rails dropdown menu how do you set which item in the list should be default?I ask because in the past I've been just putting a nil entry as the first item in my list of values that are going into the drop-down, but when using {:include_blank => true} the blank entry is not the default selected item, the first item from the list is.
You can use :selected
<%= f.select :title, ['1','2','3','4'], :selected => '3' %>
You have many options to populate <select> tag with <option> tags, one of them is options_for_select(container, selected = nil) which takes a selected param which should be the value of your <option> field you want to be selected by default.