F Select showing the current value in the Database - ruby-on-rails-3

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

Related

Setting multiple attributes select form Rails 3

I have a drop-down menu that lists a collection of locks. I would like to set two params when an option is selected:
:name => l.name (which works with the code below)
:device_id => l.id
<%= f.select(:name, Lock.all.collect {|l| [ l.name ] } ,{:include_blank => true} ) %>
I've tried passing the value in a hidden_field, but the field isn't aware of the lock that was selected. Any input is much appreciated.
Hi you can do this:
<%= select_tag(:name, options_from_collection_for_select(Lock.all, :id, :name, params[:name]) )%>
or in your controller add this code:
#locks = Lock.find(:all)
and in your views
<%= select_tag(:name, options_from_collection_for_select(#locks, :id, :name, params[:name]) )%>
when you submit your form that contains this select_tag the params[:name] will get the selected name from the url of your app.
Hope it helps.
So You need to get two values in controller side ?
Ex:-
I have below values in my db
ID Name
4 gggg
5 tttt
2 iiii
So dropdown will show the all names gggg,tttt,iiii if you select tttt from dropdown in server you required both 5, tttt right?
Then you need to use below code
In controller
#locks = Lock.all.map{|l| [l.name, "#{l.id};#{l.name}"]}
In your view
<%= select_tag(:name, options_from_collection_for_select(#locks) )%>
So it will send the both id and name values saparated by ";" .
In your controller you need to split by ";"

Rails selecting a field in grouped_collection_select

I'm using this code in my view to create a selection grouped_collection_select(:query, :city_id, #states, :cities, :name, :id, :name, {:selected => "Chicago"}) that looks like this:
I want to have "Chicago" selected by default. How can I get this to work?
Hi on your sample above you can select "Chicago" by defining the selected key index of chicago.
Here's an example:
#city_group =
[
["Wisoncin", [["Lake Geneva", "1"],
["Elkhart Lake", "2"]]],
["Michigan", [["Harbor Country", "3"], ["Traverse City", "4"]]],
["Indiana", [["Bloomington", "5"], ["Valparaiso", "6"]]],
["Minnesota", [["Twin Cities",
"7"], ["Bloomington", "8"], ["Stillwater",
"9"]]],
["Florida", [["Sanibel & Captiva", "10"]]],
["Illinois", [["Chicago", "11"],
["Galena", "12"]]],
]
and in your views add this:
<%= select_tag(:brand_id, grouped_options_for_select(#city_group, selected_key = "11", prompt = nil)) %>
Hope it helps! Enjoy!
Solution: 1
city = City.find_by_name("Chicago")
select(:query, :city_id, option_groups_from_collection_for_select(#states,
:cities, :name, :id, :name, city.id))
Solution: 2
city_obj = City.find_by_name("Chicago")
grouped_collection_select(:query, :id, #states, :cities, :name, :id, :name,
{:object => city_obj})
Solution: 3
#city = City.find_by_name("Chicago")
grouped_collection_select(:city, :id, #states, :cities, :name, :id, :name)
It is possible to pre-select an option, but it's not very clear in the documentation. The first parameter (here :city) has to be the name of an instance variable defined on self. The object stored in that instance variable has to have a method named after the second parameter (here: :id). Now #city.id should return the id of the city you want to have selected.
#city = City.find_by_name("Chicago")
grouped_collection_select(:city, :id, #states, :cities, :name, :id, :name)
I made you a gist with a slightly different example for better understanding. Note: the gist should be executed in a rails console, so that the include works

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'})%>

Selected option not working for select

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.

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')
)