options_from_collection_for_select selected value on form reload - ruby-on-rails

I am a novice in RoR.
I have this line in the code along with other select objects.
<%= f.select :from_branch, options_from_collection_for_select(#branches, :id, :name, #form.from_branch), {}, :disabled => params[:repoid].blank? %>
In the webpage, when there is an error in any input, the form reloads and keeps the previously entered values intact. But for the above select tag, it resets it to the first entry. There are no blanks in the collection.
Say there are three values in the collection - A, B, C. A is the first record and selected. I select C and submit the form. Due to an error in the form it reloads. Now I want this field to have C. Instead it again goes back to A.
Any thoughts on how do I achieve this?

Related

Rails 6 ActiveAdmin dynamic dropdown selection with page reload

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.

Cocoon + Rails—how to access object data on form error (for dynamic fields)

I have cocoon creating association sub-forms, one of these forms has projects and codes
Setup
/ view (in slim format)
.nested-fields.subform.project-subform
= link_to_remove_association "×", f
#project-code-container
.form_group
= f.label :project_id, "Project"
= f.select(:project_id,
#current_projects.map { |project| [project.name, project.id] },
{},
{ onChange: "updateCodes(this)" },
)
.form_group
= f.label :project_code_id, "Code"
= f.select(:project_code_id, #current_project_codes[1])
The second select should show a list of codes based on the index of the selected project. So if project 3 was selected, then the options for codes should be #current_project_codes[3]
Problem
When I hit a validation error on submit, the form is reloaded with the previous data already filled out (like a normal rails form). However, I don't know how to tell the project_code select which options it should load. Since this is a cocoon form, I don't have an object like #project to access the data.
I need to figure out what project was selected, so I can show the right codes.
On validation error/reload, how can I get a cocoon object's data?
p.s. I'm struggling to define this question, if you have ideas on how to make my question clearer, please let me know.
Credit to #arieljuod
f.object gives the form's object with its data
For my specific problem, this was the line that allowed a select to autopopulate with any pre-saved data:
f.select(
:project_code_id,
#current_project_codes[f.object.project_id || #current_projects.first.id]
)

rails association populate field from another table

Hoping this will be a straight forward question, but is anyone able to let me know the best way of populating a hidden field based on a value on another table.
I currently have 2 tables - table_numbers which has the following fields - id and value(decimal), and table_records which has an amount field.
On the form to add a new record I have the following to add a value to it
= f.association :table_number, :collection => table_numbers.order('value ASC'), :label_method => :value, :prompt => "Select a value", :label => "value"
At the moment this is populating the number_id on the records table, but displaying the value on the form when adding a record. What I would like is to get the value as well to be able to run a calculation on the value and amount.
What would be the best way to do this? Update the line above or do I need to add extra code?
Thanks
You can perfom calucaltions in before_save callback in model. If you want to display calculations, use helper and show it.

rails collection_select wrong number of argument error

I have a user_inputs table where I am storing the device subscription statuses under a column sub_status and these subscription statuses I want as drop down options Under the same name. Now after selecting one option from the drop down I want to save the id of the status in equipment_assets table under a column_name subscription_status and display the status on the browser.
I am trying collection_select for it but its not working.
<div class="pluginESV_formfield">
<%= f.label :subscription_status %><br />
<%= collection_select :sub_status,UserInput.all,:id, :subscription_status %></div>
this gives error, wrong number of arguments, Please help me with this.
here-
:sub_status is the field which has the drop down options.
UserInput is the model from which these status are coming.
:id is the index of the sub_status from the user_inputs table
:subscription_status is the column in the equipment_assets table where selected IDs will be stored. I am not getting what's wrong with the
code.
Please help me out with this.
For table equipment_assets with field subscription_status, you need to update collection_select as below:
<%= collection_select :equipment_asset, :subscription_status, UserInput.all, :id, :sub_status %>
As per the collection_select syntax, i.e.,
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
you missed the object argument which is why you got the error as wrong number of arguments. Its a mandatory argument as it helps to form select HTML element with proper id and name so that upon submission of form the selected value of select drop down will be passed in params hash correctly.

How to select the values dynamically from select box based on the previous select box?

I am sorry if i cannot make you understand.
I have two tables where the first table contains categories and the next one contains sub-categories and i have another table item where i have has_and_belongs_to_many relation with sub-category table. When i create a new item, i have to display the categories in the first select box and based on the selection, the sub-categories has to be displayed in another select box and the value has to be added. I have already another like shown below
<%= collection_select :item, :sub_category_ids, SubCategory.find(:all, :conditions => ["category_id = 7"], :order => 'sub_category_name ASC'), :id, :sub_category_name, { :selected => #item.sub_category_ids }, { :multiple => true, :name => 'item[sub_category_ids][]' } -%>
and now i want to add this one like
along with that. How can i do this?
You'd have to use AJAX to achieve what you want.
You would want to use Javascript to watch the first drop down value. When that value changes, you would want to make an ajax call to retrieve the values of the second drop down, then fill the second dropbox based on the AJAX response

Resources