Selected option not working for select - ruby-on-rails-3

I have this select which works fine, but default the select is empty and doesn't show the selected value (which is filled correctly):
<%= f.select(:relationgroup, options_for_select(#relationgroups), { :selected => #relation.relationgroup, :include_blank => true}) %>
Any idea why? Thanks!

Try it that way:
<%= f.select(
:relationgroup,
options_for_select(#relationgroups, #relation.relationgroup),
:include_blank => true
) %>
Not sure, but maybe it'll work better.
Anyway, assuming Relationgroup is some model with id and name (or any other attribute that you want to be visible in select options) attributes, and you're using default relationgroup_id foreign key in your model you'd better construct your select like that:
<% f.select(
:relationgroup_id,
options_from_collection_for_select(#relationgroups, :id, :name),
:include_blank => true
) %>
It'll choose selected value based on object.relationgroup_id where object is the model you're building form for. See docs for more information.

Related

Concatenate field names in select

I have a form that allows you to select a facility by name then writes the id of the facility to a vale in the database.
<%= f.collection_select(:transfer_to_id, Facility.all, :id, :facility_name, {:include_blank => true}, {:class => 'select'})%>
I'd like to be able to select the facility name but to the right of the facility name display the facility_address in the form. I'm not sure how to do this, possibly an array of some sort or using a helper method.
If anyone can provide some help it would be appreciated.
Here is what ended up working properly for me by creating a Class method.
def facility_name_with_facility_address
"#{facility_name} | #{facility_address}"
end
This is untested so bear with me, but you need to add a method to your Facility model:
def facility_name_with_facility_address
facility_name << " " << facility_address
end
And then in your form you want to change the following:
<%= f.collection_select(:transfer_to_id, Facility.all, :id, :facility_name, {:include_blank => true}, {:class => 'select'})%>
To this:
<%= f.collection_select(:transfer_to_id, Facility.all, :id, :facility_name_with_facility_address, {:include_blank => true}, {:class => 'select'})%>

Rails: two (or more) instances of same collection_select?

I've got a collection_select instance in a form, and I'm wondering if it's possible to have two or more instances in the same form. They'd be built from the same model, and they would save as if they were checkboxes constructed in an Article.all.each loop. To have these work
<%= f.collection_select("article_ids", Article.where(:page => 1), :id, :name) %>
<%= f.collection_select("article_ids", Article.where(:page => 2), :id, :name) %>
<%= f.collection_select("article_ids", Article.where(:page => 3), :id, :name) %>
in the form is pretty much what I'm after. It's essentially a multiple select but spread over a couple of selects. The field already accepts multiple results, but when I save the form as it is above it only records the option from the final select. Any thoughts?
Cheers!
<%= select_tag "article_ids[]",options_from_collection_for_select(Article.all.collect{|i| [i.name,i.id]),:multiple => true %>
When select multiple options in select list just give article_ids[] , it will store all ids in this array then after you write query how you would store in database.
If set the select tag is multiple true then you will select multiple options other wise you get only one selected value.
or just read below link
http://api.rubyonrails.org/?q=collection%20select
If you want to give f.select then you must give like this
<%= f.collection_select :article_id, Article.all, :id , :name %>
I just went with checkboxes to solve this, because it's truly the stuff of nightmares.
<% #articles.each do |a| %>
<%= check_box_tag("doc[article_ids][]", a.id, #doc.articles.include?(a.id), :class => "article_chooser") %> <a id="<%= a.id %>" class="name"><%= a.name %></a><br />
<% end %>

F Select showing the current value in the Database

I have the following as my f select
How do i modify to show what is in the database?
<%= f.select :phase_names, options_for_select([["Select One", "", #phase_names_string_value], "RFP Stage", "Pre Contract", "Awarded", "Unsuccessful", "Completed"]), :class => 'inputboxes' %>
Phase Names is from a second table in the database.
however each project can only sit in one phase at a time.
Thanks in advance
options_for_select(container, selected = nil)
The container is display\value combination so you need [[value,name],[value,name]] or if its the same [name]
Like ["RFP Stage", "Pre Contract", "Awarded", "Unsuccessful", "Completed"]
Now that you have the container you need something to match the selected value
#current_value = MyModel.find(1).vari # Assume MyModel table with id has col vari=Completed
Then, you can do
select_tag "select_name", options_for_select(["RFP Stage", "Pre Contract", "Awarded", "Unsuccessful", "Completed"].insert(0, "Select One"), #current_value)
Another way to do it is to have a collection of an object that holds the options with lets say :name (displayed) and :value (used as value) where selected has :phase_names = :value
f.collection_select :phase_names, [:name => "rStage", :value => "Pre Contract"] , :value, :name, {:include_blank => 'Select one'}
Which works just as activerecord classes

Rails: Adding options to collection_select

I am using a collection_select field, but need to prepend the options with some default one, wich does not represent a particular model record and is used to set the appropriet field to NULL. But I just can't find any way to do that.
If you need any more information, don't hasitate to ask.
Using Rails 3.2.3 with standard form helpers.
P.S. I know I can do something like this:
#parents = ['default_name','nil']
#parents << Model.all.map {|item| [item.name,item.id]}
But I think there is a more elegant way.
There is an :include_blank option you can pass to collection_select helper method:
f.collection_select(:author_id, Author.all, :id, :name_with_initial,
:include_blank => "Nothing selected")
There is also a similar option called :prompt, check it out too.
You can probably use select instead :
f.select(:item_id, #items.collect {|p| [ p.name, p.id ] } + ['Or create a new one like','new'], {:include_blank => 'Please select a item'})
Something like this is acceptable in your view?
collection_select :field1, :field2, #models+[Model.new(name: "default_name")], :name, :id

Rails select_tag - Setting include_blank and selecting a default value

select_tag :country_id, options_from_collection_for_select(Country.order('priority desc, name asc'), "id", "name"), { :prompt => 'Select a country', :include_blank => 'None' } %>
Does as expected, except :include_blank => 'None'. Renders an blank option. Like such:
<option value=""></option>
Second, with the select_tag. How do I specify a default value. For example, if I need the select box to select a specific country. I tried adding :selected => Country.first to no avail:
<%= select_tag :country_id, options_from_collection_for_select(Country.order('priority desc, name asc'), "id", "name"), { :prompt => 'Select` a country', :include_blank => 'None', :selected => Country.first } %>
Above always selects "Select a country".
Why?
Blank Value
I don't think this is getting enough attention on other posts:
include_blank on a select_tag does not respect the string passed to it. It only interprets it as a true/false value.
In order to set a blank value for select_tag with a specific string, you need to use prompt.
Selected Value
Because select_tag does not belong to a object, the way select does, you need to specify the selected value as part of the options. pass the selected value into the options param of the select_tag.
In your case, you are using options_from_collection_for_select to help generate those options. This method takes a fourth parameter that specifies which option should be selected.
options_from_collection_for_select(
Country.order('priority desc, name asc'),
:id,
:name,
Country.find_by_name('Canada')
)