Simple Form For - Rails - f.association options - ruby-on-rails

I have the following field in my form:
<%= f.association :account_manager, collection: Person.appear_on_register_page, :value_method => :id, :label_method => :full_name, :required => true, prompt: "Please Select", label: label %>
This gives the user a textbox, which when clicked on gives a dropdown with every record in the collection. When typing in the text box, this dropdown is narrowed to only match what is in the text box.
I've been asked to remove the dropdown until a user has started entering text - for example when first clicking on the text box no drop down appears until the first 3 letters have been entered - then all records matching those three letters appear.
I've looked through their github and can't find any options for this.. Any help is appreciated

Hope you are looking for a auto complete text box. You can achieve this using Jquery UI.
This is a good gem you can go with.
This is one of the good tutorial for that. Thanks

Related

Rails - Combination of Dropdown and Free-Text-Input

I am using Rails 4.2.1 and ruby 2.1.6
I am looking for a solution to do the following:
Have a dropdown containing a list of existing regions (Bundesland).
If I find my region in that list, select from the dropdown and I am fine.
If I do not find my region, I need to manually type in a new region into a text field. This will create a new entry in the database as well.
To have this userfriendly and easy to use without switching between fields, I prefer to have this in one field if possible.
Here is the code for my actual dropdown, but without any option for
free-text and manual input.
<br>
= ai3.input :area_id, label: "Bundesland/Kanton", :as => :select, :collection => option_groups_from_collection_for_select( #area_countries_dach, :area_regions, :name, :id, :name)
Now this needs to be expanded to a combination of a select-box (dropdown) and a free-text input field.
It would be great to have the user typing into the field the beginning chars for the region. if found, select by click or leaving the field. If not found use the already typed chars for the creation of the new region.
Any ideas on how to solution this?
Thanks.
You can manually have a text_field appear upon clicking a link/button/div if the region is not found in the drop-down.
Something like below
<div id="show_region" class="btn btn-primary">Cant Find Your Region? Manually Add It Here</div>
<%= ai3.input :some_attribute, :class => 'form-control', :id => 'input_region', placeholder: 'Enter your region' %>
Javacsript
<script type="text/javascript">
$('#input_region').hide();
$(document).ready(function() {
$('#show_region').click(function() {
$('#input_region').slideToggle("slow");
});
});
</script>
But you can't save it in the same attribute AFAIK. You need to have a different attribute for the text_field.
It would be great to have the user typing into the field the beginning
chars for the region. if found, select by click or leaving the field
This needs an autocomplete text_field rather than a normal text_field. Take a look here on how to implement it.
As Pavan mentioned, an autocomplete text field works well in this situation. I ran across the same thing recently and used twitter typeahead for it.
https://github.com/yourabi/twitter-typeahead-rails
There's lots of discussion on it elsewhere.

simple_form, manually place the field and the error label

In a simple_form (with bootstrap integration), i have a checkbox with a short text.
The text is 'I've red'.
In the case the user doesn't check the checkbox, the validation error is:
'You have to confirm that you've read'.
As you can see the look isn't very good, since i would like at least the checkbox to be alligned with the 'I've read' label and not with the error message.
This is the code:
<%= f.input :number_of_items, :label => "I've read" %>
So i tried the inline label, that is:
<%= f.input :number_of_items, :label => false, :inline_label => "I've read" %>
but now the result is as follows.
That is: the checkbox is inline with the text, and this is correct... but as you can see the error is rendered at the turn of the lines!!!
I'm looking for a solution to this specific issue.
Then I'm looking for a further customization possibility that is a way to render independently a control and its error message.
This would help me achieve a better, ad-hoc rendering where none of the defaults fits well.

In simple_form, can't submit boolean as radio_button if the field is disabled

I am using simple_form 2.0. I have a Boolean field 'stock' which I am trying to submit as radio buttons.
<%= f.input :stock , :as => :radio_buttons, :collection => [['Purchase Indent', false],
['Stock', true]], label:"Shipments From" , :disabled => true%>
The stock is marked as false before rendering the form.
Once I submit the form the stock itself is missing from the parameter and I get this error.
Because I am validating stock's inclusion.
validates_inclusion_of :stock, :in => [true, false]
It works fine if i don't disable the field. But I don't want user to be able to change it.
Please help.
Update
The reason is that, the disabled fields are never sent. http://www.w3.org/TR/html401/interact/forms.html#h-17.12
Seems like making it read-only will help.
https://github.com/plataformatec/simple_form/pull/367
But, the news is radio buttons can't be made read only.
Why can't radio buttons be "readonly"?
One option is to separate the buttons and only disable the unselected option:
<%= f.input :stock , :as => :radio_buttons, collection: [['Purchase Indent', false]], label:"Shipments From" %>
<%= f.input :stock , :as => :radio_buttons, collection: [['Stock', true]], label:"" , :disabled => true %>
Another option would be to add a hidden input with the desired value.
Remember not to trust user submitted data.
I don't think you should build it like this, because a hacker can just change the HTML / submit an artificial request even if you disable the form elements. Hidden form elements don't fix this, as anyone with a dom explorer can change the values. Of course, if your model checks and rejects this kind of thing it's not such a big problem.
So to fix the particular problem, just do the visuals as you have already, and re-insert the expected value in your controller's update or create action.
For more info there's lots online e.g. owasp, but I liked the book "How to break web software" from a few years back by some guys at Google.

Adding description paragraph to ActiveAdmin /new Page

I have a page set up on ActiveAdmin and when the user clicks the "Create One" button, I want to add a short textbox on the /new page (either at the top or part of the actual form, whatever's easier) explaining what needs to be put into the form.
How can I do this?
Active Admin gives complete control over the output of the form by creating a thin DSL on top of the fabulous DSL created by Formtastic (http://github.com/justinfrench/formtastic).
And you can add :hint for each form input like this:
form do |f|
f.inputs 'Details' do
f.input :title, :required => true, :hint => "This field should be filled in"
end
f.inputs 'Advanced' do
f.input :keywords, :hint => "Example: ruby, rails, active-admin"
...
end
end
Take a look Formtastic documentation, there is lot of capabilities...
You can simply add new description column in your model or you can use active admin comments here is comments description for active admin.
Hope it would help you.

How to have a collasped drop down list in rails simple_form

In my app, there are two models: rfq and standard. Their relationship is many-to-many. In rfq creating screen, the code below displays a list of available for selection in drop down list:
<%= simple_form_for #rfq do |f| %>
<%= f.association :standards, :collection => Standard.active_std.all(:order => 'name'), :label_method => :name, :value_method => :id %>
<% end %>
The problem is that the list is not collapsed, which means there are multiple standards displayed in a multi-line boxes. How can I reduce the box to one line only?
Thanks.
UPDATED: here is the screen shot of multiple line list box:
It's creating a multi-select because one rfq can have many standards, so it allows you to ctrl-click to select many standards.
You could try adding :input_html => { :size =>'1' } but I'm not sure that will preserve the scrollbar. It definitely won't drop down.
Here's someone else who wanted to do the same thing: HTML muliple select should look like HTML select. One of the answers refers to a Dropdown Check List implemented in jQuery, but that would take some work to integrate with SimpleForm.
SimpleForm has a very helpful Google Group--you might get more ideas there:
http://groups.google.com/group/plataformatec-simpleform
You can add as: :collection_select
Use
=f.collecion_select, model_associated_ids, collection, value, label
in your is like this
=f.collection_select, :standard_ids, Standard.active_std.all, :id, :name
you can find more info here
https://github.com/plataformatec/simple_form

Resources