I have working on project using ruby on rails. I have created a form with many fields and one field is for jobs where I want to choose more then one job but now I am not able to choose more then one job with following code. Working with ruby on rails 3 and HAML. I also mention multiple true but still its not working.
= f.select :jobs, options_from_collection_for_select(Demojob.all, 'name','name' ), :multiple => true
If you check the implementation of the form builder select method (github), you'll see that the method signature is:
select(method, choices = nil, options = {}, html_options = {}, &block)
multiple flag should be passed using html_options hash, not options. In your case, it should be:
f.select :jobs, options_from_collection_for_select(Demojob.all, 'name', 'name'), {}, :multiple => true
Or, even better, if you prefer new hash syntax:
f.select :jobs, options_from_collection_for_select(Demojob.all, 'name', 'name'), {}, multiple: true
Lastly, there's no need to use options_from_collection_for_select with form builder, you can simply pass the options as arrays:
f.select :jobs, Demojob.all.collect { |job| [job.name, job.name] }, {}, multiple: true
Cheers!
Related
I am trying to override Rails' select method with a custom form helper, so that all select boxes get a disabled attribute by default:
class LabelFormBuilder < ActionView::Helpers::FormBuilder
def select(method, choices, options = {}, html_options = {})
html_options.reverse_merge! :disabled => true
super(method, choices, options = {}, html_options = {})
end
end
The problem is that the code is not working and that it doesn't change anything in the way the select boxes are rendered. It doesn't throw an error either.
This would be the view code that I use to call the function:
<%= f.select(:person_id, current_user.person_names, {:prompt => 'Please select...'}) %>
What am I missing here?
Thanks for any help...
Try it maybe with
<%= f.select(:person_id, current_user.person_names, {}, {:prompt => 'Please select...'}) %>
I currently have a multiple select box in a rails form that looks like this:
= select_tag :in_all_tags, options_from_collection_for_select(Tag.where(:project_id => #project.id), :id, :name, #in_all_tags_param), { :id => "tags", :tabindex => "3", "data-placeholder" => "Choose Tags", :multiple => "multiple" }
Where
#in_all_tags_param = params[:in_all_tags]
The problem is, #in_all_tags_param will only populate the select form with the last value from params[:in_all_tags]. So, if the url string reads in_all_tags=5&in_all_tags=8, the pre-selected value in the multiple select will only be 8.
From what I understand, the way around this is to append [] to the field name for multiple params, so that :in_all_tags becomes in_all_tags[]
BUT, when I try this, submitting the form returns:
Expected type :default in params[:in_all_tags], got Array
Any suggestions appreciated.
Cheers...
You need to add a :name element to the same hash with :multiple => true in it. So I use something similar for Genres on an app for mine and I do { :multiple => true, :name => "lesson[genre_ids][]" }. The name has to be model[attribute][].
Is it possible to somehow use the :selected option that you'd use on a normal select view helper with the grouped_collection_select function? I'd like to set the value that gets pre-selected in my list. I've tried passing in :selected as an option with no luck!
Here's some code snippts of my tests:
grouped_collection_select 'user[subscription_attributes]', :subscription_plan_id, Trade.order(:name).all, :subscription_plans, :name, :id, :display_name, { :include_blank => true, :selected => 5 }
grouped_collection_select 'user[subscription_attributes]', :subscription_plan_id, Trade.order(:name).all, :subscription_plans, :name, :id, :display_name, :include_blank => true, :selected => 5
Neither version works. No selected is set. I'm using this to set a value for a nested model. I'm using the railscasts dynamic select list methods: http://railscasts.com/episodes/88-dynamic-select-menus-revised
I couldn't get formtastic to play nicely with the group selects so I had to do it by hand but I don't keep this value selected when a user fails validations. I'd like to keep this set when they fix validation errors.
I just ran across the same problem and solved it using the option_groups_from_collection_for_select helper and the select helper documented at: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html.
The first step is to create the grouped options. Taking your example, it should look like:
<% options = option_groups_from_collection_for_select(Trade.order(:name).all,
:subscription_plans, :name, :id, :display_name, 5) %>
Then I created the select object like:
<%= select('user[subscription_attributes]', :subscription_plan_id, options,
include_blank: true) %>
You could write it all out in one line, I just broke out the options into a separate variable to illustrate the two different methods.
Maybe too late, but the API documentation of grouped_collection_select states:
'The value returned from calling method on the instance object will be selected.'
So, you don't even have to specify a :selected option, since Rails will automatically select based on the current value of your attribute.
If subscription_plan_id has value 5, then that's what will be selected.
If that's supposed to be a default value, then you can set it with an after_initialize in your model.
Actually you need to send in options include_blank for example
<%= grouped_collection_select :id, model.all, options = {:include_blank => 'Selecione'}%>
How is it possible to preselect a country in the dropdown list generated by the country_select or localized_country_select helpers? The following code nicely creates a list of countries, but the :selected => :ch part does not have the expected effect of preselecting Switzerland (ch):
<%= localized_country_select(:user, :country, [], :selected => :ch) %>
Update: the following seems to be a better choice for achieving this.
localized_country_select_tag(name, selected_value = nil, priority_countries = nil, html_options = {})
I have a Rails 2.3 web application that uses the collection_select helper with :multiple => true to handle a habtm relationship. This is working fine to set one or multiple values, however I have not figured out how to allow to REMOVE all selections.
Code:
<%= f.collection_select :category_ids, Category.find(:all), :id, :name,
{ :selected => #entry.category_ids },
{ :multiple => true, :name => 'entry[category_ids][]' }
%>
Once the user has ever set a category for an entry, how would I go about allowing it to be removed, so that this entry has no category? Is this possible with collection_select or would I need to add a checkbox to handle this specially?
P.S: I already tried with :prompt, :include_blank and :allow_blank, but as far as I could see neither of them did anything.
In your controller's update action, put in the following line:
params[:entry][:category_ids] ||= []
before the call to Entry.find.