Currently if you call #team.user_ids it returns "1,3,7" as opposed to [1,3,7]. I need it to be like that for other reasons, but it's breaking the front-end collection_select. I've tested and it works fine with an array, so now the only step left is to manually choose what items are selected considering I can't do it the Rails default way.
= f.collection_select :user_ids, #company.users, :id, :username,
{ :include_blank => true }, { :multiple => true, :class => "chzn-select" }
How would I go about doing this? Thanks!
I solved it by adding :selected => #team.users.map(&:id)
Related
Ok, I have a multi select box controlled by chosen jquery plugin.
I can get multi select working without ajax, and ajax working without multiselect, but not the two together.
Here multi select works, but reloads the whole page when an item is selected (remote not working)
<%= f.collection_select :genre_cont_any, [t('genre.alternative'), t('genre.blues'), t('genre.children'), etc etc etc.............. ], :to_s, :to_s, {}, { :multiple => true, remote: true, onchange: "this.form.submit();" } %>
Here ajax works fine, reloading my list only, but I can only select one option at a time (multiple not working)
<%= f.collection_select :genre_cont_any, [t('genre.alternative'), t('genre.blues'), t('genre.children'), etc etc etc.............. ], :to_s, :to_s, {},:data => { :multiple => true, :remote => true, onchange: "this.form.submit();" }} %>
I want to be able to multi select, and with each new addition, to send an ajax request and reload the list.
Any advice on linking everything would be great! Thanks!
Figured it out:
<%= f.collection_select :genre_cont_any, [t('genre.alternative'), t('genre.blues'), t('genre.children'), etc etc etc.............. ], :to_s, :to_s, {},:data => { :remote => true, onchange: "this.form.submit();" }, :multiple => true %>
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);
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})%>
Below is my select-form that works properly.
When the user loads the page it shall show an initial ‘select one...’ with value null or ‘’.
I tried to add it to the Object but wasn’t able to and would be glad to get help!
Thanks a lot!
In my view:
= select_tag 'incident[fault_id]' , options_from_collection_for_select( Fault.all, :id, :label)
I use Rails 3.2 and HAML
Update:
By chance I found something really sweet:
include_blank: 'select one...'
or completely
= f.collection_select :fault_id, Fault.order(:label), :id, :label, include_blank: 'select one...'
In case one likes that too...
Reference: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html
options_from_collection_for_select returns a string of option tags that have been compiled by iterating over the collection and assigning the result of a call to the value_method as the option value and the text_method as the option text.
So just prepend it with "select_one" option string without value:
= select_tag 'incident[fault_id]', content_tag(:option,'select one...',:value=>"")+options_from_collection_for_select( Fault.all, :id, :label)
:prompt is a property of select_tag not of options_from_collect_for_select so
select_tag("sales_rep[manufacturer_id]", options_from_collection_for_select(#manufacturers, "id", "name"), { :prompt => 'Select Manufacturer' })
You could try the following:
collection_select(:sales_rep, :manufacturer_id, #manufacturers, :id, :name, { :prompt => 'Select Manufacturer' })
I've looked at How do I set the HTML options for collection_select in Rails? and I'm sure I'm missing something obvious, but I can't get this to work.
My select currently looks like:
<%= f.collection_select :broadcast_id, broadcasts, :id, :to_s,
:include_blank => 'Broadcast on...' %>
and I've tried including :class => 'prevent_collapse', which does nothing, as well as {:class => 'prevent_collapse'}, which gives me an error.
If anyone can point out how to do this, I'll be super grateful!
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
=>
f.collection_select :broadcast_id, broadcasts, :id, :to_s,
{:include_blank => 'Broadcast on...'}, {:class => 'prevent_collapse'}
And what error do you have?
And does broadcast item has got :to_s method? It will return class name, as I think.
Is that field :include_blank => {}, compulsory ? I tried with :include_blank => false and it worked. I wonder if we can avoid it ?