Rails Form Select Requirement with partial - ruby-on-rails

I'm creating a form with a .select field that loads a list of states via partial. The requirement isn't being enforced on state and I'm not sure why. It lets you submit the form with the default blank value 'State'
Would appreciate any help figuring out where my syntax is wrong on this form? If this looks foreign, using SLIM instead of HTML.
= f.select :state, nil, include_blank: 'State', required: true # not working
= render partial: 'addresses/states'
= f.text_field :zip, placeholder: 'Zip', required: true, pattern:'[0-9]*' # works
The states partial looks like this:
option value="AL" AL
option value="AK" AK
option value="AZ" AZ
option value="AR" AR
...

Related

Rails - unable to convert simple select to chosen field

What I have
I have a simple select field on a view, its not part of any form, selecting any option sends a remote request to the route.
There are a bunch of applications in a view listed in table and each application has this dropdown which user can use to assign a tag to the application. The code of this select_tag is as following.
<%= select_tag 'application_tag',
options_from_collection_for_select(HiringTag.order(:name), :id, :name, application.tag),
prompt: "Assign a tag to the application",
class: "form-control input-block-level chzn-select",
id: "hiring_tag_dropdown",
data: {
remote: true,
url: "applications/"+application.id.to_s+"/assign_tags",
method: 'post'
}
%>
Right now this works fine as it is and tag is being assigned to the application.
What I am trying to do
I am trying to convert this select_tag to multi select field using collection_select and the gem I am using is gem 'chosen-rails'. Looking at the documentation and several other blog articles it seemed like a straight forward job but I just cant get the field to appear the way it should. This is what I have done.
<%= collection_select :application_id, :application_tag, HiringTag.order(:name), :id, :name, {}, {include_blank: true, multiple: true, class: 'form-control input-block-level chzn-select'} %>
This is what the field looks like in view
and this is how i am expecting it to look like
I also added the javascript part but that did not help either
$('.chzn-select').chosen();
Question
What am i doing wrong here :)

How to make a collection_select in rails that only displays options with a specific attribute

I'm building an app where a user can customize components in a pc(system), based on components that are available in their respective tables (eg. motherboards table, cpus table.. etc)
I would like the user to be able to select the components via a dropdown select in the system form.
I have been able to achieve this by using collection_select like this
<%= collection_select(:system, :motherboard_id, Motherboard.all, :id, :model, prompt: true) %>
However, the collection_select displays all components in the table, and I wish to only display those components that have an available: true attribute.
I have tried
<%= collection_select(:system, :motherboard_id, Motherboard.any? {|mobo| mobo.available?} , :id, :model, prompt: true) %>
which results in undefined method 'map' for false:FalseClass screenshot:
I thought about adding a before_save callback that checks each items availability, but if that's not the only way to get it to work, I think that would be a poor decision in terms of UX
You can use
<%= collection_select(:system, :motherboard_id, Motherboard.where(available: true), :id, :model, prompt: true) %>

Rails collection_select default select

In Rails 4 in view I have
<%= form_for #person do |f| %>
<%= f.collection_select :country_id, Country.order(:name), :id, :name, include_blank: "Select your country" %>
...
<% end %>
I'd like "Select your country" to be selected as default whenever the page is loaded. One way is to use javascript (select it after the dom is loaded). Is there an easier way like adding an option to collection_select?
Thanks.
As per the docs, it's the prompt option in the options argument:
collection_select(:post, :author_id, Author.find(:all),
:id, :name_with_initial,
{:prompt => 'Please select the author of this post'})
With collection_select on a form builder we omit the first argument, so in this case:
f.collection_select :country_id, Country.order(:name), :id, :name, {prompt: 'Select your country'}
I've 100% confirmed this as working on my own app running Rails 4.1.6, where prompt and include_blank do the same thing.
The way this works is Rails injects a null-valued <option> as the first item in the generated <select> (this is because the HTML spec has nothing analogous to placeholder on text inputs for select inputs).
Reasons this may fail:
Rails does not mark the prompt option with the selected attribute, and I suspect some browsers may choose to render their own blank entry instead of the first in the list
If, for existing records, Rails determines that the current record's country_id matches an element in the list it will mark that one as selected. This is expected behaviour but can be a pain if you're doing anything non-standard.
If you're being bitten by these problems your options are to build the form manually (the method options_from_collection_for_select may be of use here) or do it in javascript. There is also an undocumented default attribute you can add to an <option> tag but it's not in the spec and browser support may be patchy, and you'd still have to build the form manually.

simple_form doesnt prevent sending request without required fields

i have this form
= simple_form_for #category.fields.build, url: category_fields_path(#category) do |f|
= f.input :kind, collection: Field::FIELD_TYPES, prompt: "Choose field type"
= f.input :description
= f.submit "Add field"
and this field model
class Field < ActiveRecord::Base
FIELD_TYPES = %w(integer float date string text)
validates :description, presence: true
validates :kind, presence: true
belongs_to :category
end
when i leave 'description' field empty, no request is send and i get notice 'Please fill out this field'. which is what i want. on the other hand, when description is filled in but kind is not, a request is still send to the 'create' action! No field gets created, but 'description' needs to be filled in again. there should be no request in such situation. any idea how to fix this?
Though, I don't have exact answer to Your problem, but You should start with checking HTML output. Simple from relies on HTML5 to provide front-end validation. All inputs should have required attribute, to have validation enabled. Maybe there is a bug, and in this particular case simple_form does not output required attribute.
Another thing to take in account as it is HTML5, consult browser support: http://caniuse.com/#feat=form-validation . Theoretically it's possible that You are testing on browser that has limited support for form validations.
If You discover that simple_from did not output required for Your kind fuel, try forcing it:
= f.input :kind, collection: Field::FIELD_TYPES, prompt: "Choose field type", required: true
I got my answer at Simple Form github's issue topichere. to sum up, problem was prompt, validation is not (yet?) working with it correctly, solution is to replace it, eg like this:
= f.input :kind, collection: Field::FIELD_TYPES, include_blank: "Choose field type", label: false

Rails - binding versus not binding a rails collection select

So I was experiencing an error when I was attaching a collection_select to my form_for object like so:
<%= f.collection_select(:city_id, #cities, :id, :name, {:prompt => "Select a City"}, {:id => 'cities_select', multiple: true}) %>
and getting the error:
undefined local variable 'city_id'
But when I don't bind the select like so:
<%= collection_select(nil, :city_id, #cities, :id, :name, {:prompt => "Select a City"}, {:id => 'cities_select', multiple: true}) %>
It works fine.
I just want to understand the theory behind why one works and the other doesn't?
I think what's tripping you up, primarily, is the concepts you have of what's going on here.
Nothing is “binding” anything to anything by calling a method on a form helper object. There are form helper methods, like collection_select, that can be used to build HTML elements. There are form builders that have methods, like collection_select that build HTML form elements for a form tied to an object.
The issue you're having here is that the FormOptionsHelper#collection_select method and the FormBuilder#collection_select method are not the same method and do not accept the same arguments:
FormOptionsHelper#collection_select vs FormBuilder#collection_select
Pay particular attention to the arguments provided. It's also worth noticing that FormBuilder essentially delegates this work to the template (i.e. FormOptionsHelper) and adjusts the arguments as needed.

Resources