Rails: form_for and radio buttons - ruby-on-rails

I have the following in my form_for
<div class="multiple_students">
<%= f.label :multiple_classes, "Do you teach multiple classes?" %>
<%= f.radio_button :multiple_classes, 1, checked: #user.multiple_classes?, class: 'multiple_classes', data: { question: 'What is your average class size?' } %> <%= f.label :multiple_classes, "Yes", class: 'multiple_classes' %>
<%= f.radio_button :multiple_classes, 0, checked: #user.multiple_classes?, class: 'multiple_classes', data: { question: 'How many kids do you teach?' } %> <%= f.label :multiple_classes, "No", class: 'multiple_classes' %>
</div>
<div class="number_of_students hide">
<%= f.label :students %>
<%= f.text_field :students, :class=>"student_count required digits" %>
</div>
For some reason the "No" is ALWAYS checked even if the multiple_classes attribute on the user is selected a true in the database (multiple_classes is a boolean type in the database)

Ok if you want to use only the radio_buttons, check the following and let me know if thats worked.
<%= f.radio_button :multiple_classes, "1", checked: #user.multiple_classes?, class: 'multiple_classes', data: { question: 'What is your average class size?' } %> <%= f.label :multiple_classes, "Yes", class: 'multiple_classes' %>
<%= f.radio_button :multiple_classes, "0", checked: #user.multiple_classes?, class: 'multiple_classes', data: { question: 'How many kids do you teach?' } %> <%= f.label :multiple_classes, "No", class: 'multiple_classes' %>
put the 1 and 0 with quotes and try....
Its not an answer... its only an idea...

The problem with always checked "No" is in a typo.
If you revert #user.multiple_classes? for "No", checkbox will be unchecked if #user.multiple_classes? is true, so the next code chunk should work fine:
<%= f.radio_button :multiple_classes, 1, checked: #user.multiple_classes?, class: 'multiple_classes', data: { question: 'What is your average class size?' } %> <%= f.label :multiple_classes, "Yes", class: 'multiple_classes' %>
<%= f.radio_button :multiple_classes, 0, checked: !#user.multiple_classes?, class: 'multiple_classes', data: { question: 'How many kids do you teach?' } %> <%= f.label :multiple_classes, "No", class: 'multiple_classes' %>

Related

bootstrap on rails: changing from select to radio on a form

rails 6.0.3
bootstrap 5
I'm sorry if there is a simple answer, or if it's not even possible to change the following:
I currently have a select dropdown on my form,
<div class="field mb-3">
<%= form.label :Please_select_document_type %>
<%= form.select(:priority, [['Critical'],['Moderate'], ['Low']], { :include_blank => '-- Select One --' }, {class: "form-check"}) %>
</div>
Drop down menu
I have written below a radio button with the three same priority levels. How do I get the radio button to store the correct value to priority when it is selected?
<%= form.label :priority %><br>
<div class="btn-group" data-toggle="buttons-radio">
<%= form.radio_button :priority, 'Low', type: "radio", class: "form-check btn-check", name: "options-outlined", id: "success-outlined", autocomplete: "off", checked: true %>
<%= form.label :priority, class: "btn btn-outline-success", for: "success-outlined", value: "Low" %>
<%= form.radio_button :priority, 'Moderate', type: "radio", class: "form-check btn-check", name: "options-outlined", id: "warning-outlined", autocomplete: "off" %>
<%= form.label :priority, class: "btn btn-outline-warning", for: "warning-outlined", value: "Moderate" %>
<%= form.radio_button :priority, 'Critical', type: "radio", class: "form-check btn-check", name: "options-outlined", id: "danger-outlined", autocomplete: "off" %>
<%= form.label :priority, class: "btn btn-outline-danger", for: "danger-outlined", value: "Critical" %>
</div>
Radio button
The radio buttons should have the name <obj>['priority']. The code in the question does not work since it uses a different name for each radio button. Remove the name key from the radio_button method call and let Rails add it for you.
<%= form.radio_button :priority, 'Low', type: "radio", class: "form-check btn-check", id: "success-outlined", autocomplete: "off", checked: true %>
<%= form.label :priority, class: "btn btn-outline-success", for: "success-outlined", value: "Low" %>
<%= form.radio_button :priority, 'Moderate', type: "radio", class: "form-check btn-check", id: "warning-outlined", autocomplete: "off" %>
<%= form.label :priority, class: "btn btn-outline-warning", for: "warning-outlined", value: "Moderate" %>
<%= form.radio_button :priority, 'Critical', type: "radio", class: "form-check btn-check", id: "danger-outlined", autocomplete: "off" %>
<%= form.label :priority, class: "btn btn-outline-danger", for: "danger-outlined", value: "Critical" %>

How to create a nested Form of separate model with many_to_many relationship in Rails?

Need to create one form with entries for 2 different models and establish a join table relationship.
Models:
class Account < ApplicationRecord
has_many :account_authorizations
has_many :authorizations, through: :account_authorizations
accepts_nested_attributes_for :authorizations
end
class Authorization < ApplicationRecord
has_many :account_authorizations
has_many :accounts, through: :account_authorizations
end
class AccountAuthorization < ApplicationRecord
belongs_to :account
belongs_to :authorization
end
Model authorization already has several instances in my DB. There are only 2 attributes for authorization: auth_name and id.
My form initializes with Account. Within this form, I need to have several checkboxes for Authorization entries. I understand fields_for when using a related model but unclear how to implement the check_box method here and capture the values.
Here's what my views page looks like so far:
<% states = [ 'AL', 'AK', 'AS', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DC', 'DE', 'DC', 'FL', 'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MH', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'MP', 'OH', 'OK', 'OR', 'PA' 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT' 'VA', 'WA', 'WV', 'WI', 'WY'] %>
<% ppu = ['Employment', 'Insurance', 'Law Enforement'] %>
<div class="row justify-content-center">
<div class="col-lg-16">
<h3>Create New Account</h3>
</div>
</div>
<div class="row justify-content-center">
<div class="col-lg-16">
<%= form_with(url: accounts_path, model: #paraaccount, local: true) do |f| %>
<%= f.text_field :account_name, class: "form-control", placeholder: "Account Name" %>
<%= f.text_field :account_address_line1, class: "form-control", placeholder: "address line 1" %>
<%= f.text_field :account_address_line2, class: "form-control", placeholder: "address line 2" %>
<%= f.text_field :account_address_city, class: "form-control", placeholder: "account city" %>
<%= f.select :account_address_state, options_for_select(states), { :prompt => true }, class: "form-control", include_blank: true, placeholder: "state" %>
<%= f.text_field :account_address_zipcode, class: "form-control", placeholder: "account zipcode" %>
<%= f.text_field :contact_first_name, class: "form-control", placeholder: "contact first name" %>
<%= f.text_field :contact_last_name, class: "form-control", placeholder: "contact last name" %>
<%= f.text_field :contact_phone, class: "form-control", placeholder: "conact phone" %>
<%= f.text_field :contact_email, class: "form-control", placeholder: "contact email" %>
<%= f.select :account_ppu, options_for_select(ppu), { :prompt => true }, class: "form-control", include_blank: true, placeholder: "ppu" %>
<%= f.text_area :account_notes, class: "form-control", placeholder: "Notes..." %>
<div class="d-flex justify-content-between flex-wrap">
<%= f.fields_for :authorization do |auth| %>
<div class="order-3 p-2 bd-highlight">
# THIS IS WHERE I'M CONFUSED
<%= auth.label #need to get attribute `auth_name` for the label here %>
<%= auth.check_box #need to implement checkboxes here for each entry in `Authorizations` %>
</div>
<% end %>
</div>
<%= f.submit "Create", class: 'btn btn-success' %>
<% end %>
</div>
</div>
I need to capture the values of the checked boxes and use id of Authorization in params to create a join table relationship to Account on creation of the new Account instance.
Hope this makes sense.
you can use collection_check_boxes
<%= form.collection_check_boxes(:authorization_ids, Comment.all, :id, :auth_name) %>
in controller add authorization_ids: [] to permitted params
collection_singular_ids

"Undefined" placeholder on f.select in form

I have a form
<%= form_for #user, url: contact_path do |form| %>
<%= form.select(:email, User.all.map(&:email), {}, { class: 'my-form' }) %>
<% end %>
which works well but has placeholder "Undefined" in start position.
I tried to get rid of that with
<%= form.select(:email, User.all.map(&:email), {placeholder: "Select email"}, { class: 'my-form' }) %>
or
<%= form.select(:email, User.all.map(&:email), {prompt: "Select email"}, { class: 'my-form' }) %>
but still same. Any ideas?
<%= form.select :email, options_for_select(User.all.map(&:email)), include_blank: "whatever your prompt says" , class: 'my-form' %>
Experienced same issues today and this is what I did for one of our projects recently.
<%= f.select :category, options_for_select(Category.all.collect { |c| [c.name, c.id] }), { include_blank: 'Select category' }, { class: 'custom-select' } %>
Try changing the values according to your values.
This is how you add class and placeholder in a form.select element in Rails.

Rails f.select not passing values

Here is code sample:
<%= form_for #article, html: { class: "form-horizontal" } do |f| %>
<div class="form-group">
<%= f.label :keywords, class: 'col-md-1 control-label' %>
<div class="col-md-3">
<%= f.select :keywords, ['test_1', 'test_2', 'test_3', 'test_4', 'test_5'], {}, { :multiple => true, :size => 10, :class => 'form-control' } %>
</div>
</div>
<% end %>
When I set multiple to 'false' it works just fine, but if I set it to true (for multiple select), it just doesn't pass any data. If I have verification it gives me an "keyword is empty" error and if I remove validation - it is just empty. Any ideas?
<%= f.select :keywords, options_for_select([['test_1','test_1'], ['test_2','test_2'], ['test_3','test_3'], ['test_4','test_4'], ['test_5','test_5']]), {}, { :multiple => true, :size => 10, :class => 'form-control' } %>

Populating checkboxes based on db in Rails 4

I'm working on this reconciliation system where users can choose which fields they would like to use when importing records. I finally got the select to pre-populate with the correct info but can't for the life of me get the checkboxes to do the same. Here's my form:
#views/match_rule/edit.html.erb
<%= form_for(#match_rules) do |f| %>
<%= f.label :name, "Name" %>
<%= f.text_field :name, placeholder: "Rule Set Name" %>
<%= f.label :algorithm, "Choose an algorithm" %>
<%= f.select :algorithm, build_algorithm_select_options %>
<%= f.label :match_fields, "Available fields" %>
<% #field_options.each do |field| %>
<%= f.check_box :match_fields %>
<%= f.label :match_fields, "#{field.last}" %><br/>
<% end %>
<%= f.submit "Save", class: "button primary" %>
<%= link_to "Cancel", match_rule_index_path, class: "button" %>
<% end %>
Currently the corresponding controller is simply building a hash of fields so not to clutter up the view with repetitive code
#controllers/match_rule_controller.rb
def edit
#field_options = {
name: "Name",
email: "Email",
phone: "Phone Number",
gender: "Gender",
marital_status: "Marital Status",
dob: "Date of Birth",
campus: "Campus",
address: "Street Address",
city: "City",
state: "State",
zip: "Zip"
}
end
My model is serializing the fields
#models/match_rule.rb
class MatchRule < ActiveRecord::Base
has_many :inboxes
enum algorithm: [ :adjustable, :classic_minimal, :classic_standard ]
serialize :match_fields
end
and my database fields are storing all the options that should populate the checkboxes in the match_fields column
If it helps to see how I seeded the db with an array here's that:
#db/seeds.rb
classic_minimal = MatchRule.create(algorithm: 'classic_minimal', match_fields: ['name', 'email', 'phone', 'gender', 'zip'], name: 'Classic Minimal')
classic_standard = MatchRule.create(algorithm: 'classic_standard', match_fields: ['name', 'email', 'phone', 'gender', 'marital_status', 'dob', 'campus', 'address', 'city', 'state', 'zip'], name: 'Classic Standard')
adjustable = MatchRule.create(algorithm: 'adjustable', match_fields: ['name', 'phone', 'campus', 'marital_status', 'zip'], name: 'Adjustable')
I've checked out similar questions on here, the one closest to helping me have a breakthrough was the answer to this question: Set checkboxes on edit method in Rails 4 where he says to use .include? to determine the checked status, but I'm not sure what I would put in the params because I need to parse through the hash inside match_fields for each and every field and that would get pretty gnarly.
Any advice or guidance is greatly appreciated, thanks!
UPDATE
Update! Tried using fields_for but still not posting or populating.
<%= f.label :match_fields, "Available fields" %>
<%= fields_for :match_fields do |field| %>
<%= field.check_box :name %>
<%= field.label :name, "Name" %><br/>
<%= field.check_box :email %>
<%= field.label :email, "Email" %><br/>
<%= field.check_box :phone %>
<%= field.label :phone, "Phone" %><br/>
<% end %>
now when I check params on update in the console, it's saying:
"match_fields" => {
"name" => "1",
"email" => "1",
"phone" => "0"
},
but not actually posting or populating.
Figured it out.
<%= form_for(#match_rules) do |f| %>
<%= f.label :name, "Name" %>
<%= f.text_field :name, placeholder: "Rule Set Name" %>
<%= f.label :algorithm, "Choose an algorithm" %>
<%= f.select :algorithm, build_algorithm_select_options %>
<%= f.label :match_fields, "Available fields" %>
<%= f.check_box :name, { checked: #match_rules.match_fields.include?("name") } %>
<%= f.label :name, "Name" %><br/>
<%= f.check_box :email, { checked: #match_rules.match_fields.include?("email") } %>
<%= f.label :email, "Email" %><br/>
<%= f.check_box :phone, { checked: #match_rules.match_fields.include?("phone") } %>
<%= f.label :phone, "Phone" %><br/>
<%= f.check_box :gender, { checked: #match_rules.match_fields.include?("gender") } %>
<%= f.label :gender, "Gender" %><br/>
<%= f.check_box :marital_status, { checked: #match_rules.match_fields.include?("marital_status") } %>
<%= f.label :marital_status, "Marital Status" %><br/>
<%= f.check_box :dob, { checked: #match_rules.match_fields.include?("dob") } %>
<%= f.label :dob, "Date of Birth" %><br/>
<%= f.check_box :campus, { checked: #match_rules.match_fields.include?("campus") } %>
<%= f.label :campus, "Campus" %><br/>
<%= f.check_box :address, { checked: #match_rules.match_fields.include?("address") } %>
<%= f.label :address, "Street Address" %><br/>
<%= f.check_box :city, { checked: #match_rules.match_fields.include?("city") } %>
<%= f.label :city, "City" %><br/>
<%= f.check_box :state, { checked: #match_rules.match_fields.include?("state") } %>
<%= f.label :state, "State" %><br/>
<%= f.check_box :zip, { checked: #match_rules.match_fields.include?("zip") } %>
<%= f.label :zip, "Zip Code" %><br/>
<%= f.submit "Save", class: "button primary" %>
<%= link_to "Cancel", match_rule_index_path, class: "button" %>
You have to pass the .include? method inside the options hash. Now I just need to find a cleaner way to put this in the view.

Resources