Rails: Formtastic: Select-Boxes without primary blank field - ruby-on-rails

It's about Rails and Formtastic.
How can I add a select box with formtastic without an initial/primary blank field? So that the initially selected item is the first item with content.

Have you tried :include_blank => false ?
According to this (line 718) http://github.com/justinfrench/formtastic/blob/master/lib/formtastic.rb that should work.

You resolve this one of two ways:
First option: In each select box, specify if there should be a blank line or not. Options are:
<%= f.input :author, :as => :select, :include_blank => false %>
<%= f.input :author, :as => :select, :include_blank => true %>
<%= f.input :author, :as => :select, :include_blank => "No author" %>
The last version displays "No Author" as the display in the drop down, but submits the value as blank.
Second Option: Set the default in the config/initializers/formtastic.rb.
# Should select fields have a blank option/prompt by default?
# Defaults to true.
Formtastic::FormBuilder.include_blank_for_select_by_default = false
By default, this is set to true and all your drop downs will have blank options in them. Set it to false, and by default they all won't.

Related

Select default value for input field on collection, simple form and chosen select [duplicate]

I am using simple_form gem, I have a countries collection, it work fine when I select the country, and updated record will have the country id stored, but, when I try to edit the record, the chosen country is not selected by default at edit form.
Here is the code at edit form:
= f.input :country_id, :collection => all_countries
Shouldn't simple_form view the selected country from the db ?
Have you tried to use the :selected => option?
:selected => selected_country_id
So,
= f.input :country_id, :collection => all_countries, :selected => selected_country_id
This will work perfectly !!!
Cheers!
I know this has been answered, but I came here looking for a similar solution for a collection of check boxes. For posterity, here's how you do it:
<%= f.input :country_ids, :as => :check_boxes, :collection => [['USA', :USA], ['Japan', :JPN]], :checked => [:JPN], :include_hidden => false %>
Hope this helps someone.

How to disable a collection_select dropdown in ruby

Is it possible to have the collection_select dropdown be unclickable(disabled)? I would like the collection_select to initially have a selection displaying but be disabled and then when some other button is clicked, the collection_select is re-enabled again(via javascript) and user can now scroll through the dropdown and click on something else. I tried :disabled => true like in the following but this did not work for me:
Embedded ruby in my html
<%=
collection_select(
:post,
:post_name,
Post.all,
:post_name,
:post_name,
{:selected => Post.where(:p_address => #parentpost.p_address).select("post_name").first.post_name,
},
{:id=>'post_collection_select',
:onchange => "DoStuff(this.value); return false;",
:autocomplete => "off",
:disabled => true
}
)
%>
So far adding the :disabled => true does nothing for me. The collection_selection is behaving exactly as it was before which is the following: it displays many post names in the drop down and one is selected based on the ActiveRecord query provided
Use
:disabled => 'disabled'
instead of
:disabled => true
Then when you want to enable the select box use the following jQuery command:
$('#post_collection_select').prop('disabled',false);

remove a blank option of select field that was generated by SimpleForm

I have this piece of code:
= f.input :category, :as => :select, :label => false, :collection => Choices["Categories"]
Choices["Categories"] is just a hash of key=>value pairs.
SimpleForm generates a select field with all needed options, but it also makes the first option blank. This blank option is present in all select fields that were generated by SimpleForm.
But I don't want to have a blank option. Is there a way to get rid of it?
Something like :allow_blank_option => false?
I tried to make a presence validation of this attribute hoping that SimpleForm will detect it, but it didn't help.
You can pass a include_blank: false, include_hidden: false option:
= f.input :category, :as => :select, :label => false, :collection => Choices["Categories"], include_blank: false, include_hidden: false
or you can customize call back action in your model to remove any empty string in the array parameter, assuming a parameter with the name "types":
before_validation :remove_empty_string
def remove_empty_string
types.reject! { |l| l.empty? }
end
To remove a blank field from select it is necessary to show the selected so add selected: 1
Then set prompt to anything like prompt: "Please Select"
The final output will be
<%= select("social_links",:option_id, Option.all.collect {|p| [ p.name, p.id ] },{ selected: 1 , prompt: "Please Select"}, { class: 'form-control' , required:true})%>

Activeadmin: Setting the default prompt for all select fields

I have a two select fields and their default options are blank so I would like to set all of them to 'Choose one'. I can do it for individual fields but I want to do it in the config somehow (avoiding the redundancy).
form do |f|
f.inputs "Item" do
f.input :field_1, :prompt => 'Choose one', :foo
f.input :field_2, :prompt => 'Choose one', :bar
end
end
How do I do this? :)
Edit: These prompts would be used on many forms. It really needs to be a config thing.
Try using a array:
fields = [:field_1, :field2]
form do |f|
f.inputs "Item" do
fields.each { |field| f.input field, :prompt => 'Choose one' }
end
end

filter attributes in select field with simple_form in mongoid

I have this select input with simple_form:
<%= f.input :board, :collection => Board.where(:user_id => current_user.id)%>
I want show in select field all boards where external attribute :user_id math with current_user.id
I want to make an exception in the select field results.
I want the select field to show me the previous results except where board's title is equal to current_user's username sth like:
<%= f.input :board, :collection => Board.where(:user_id => current_user.id).except(:title => current_user.title) %>
The fix is the next query:
Board.where(user_id: current_user.id, :title.ne => current_user.username)

Resources