Concatenate field names in select - ruby-on-rails-3

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

Related

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

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 %>

Simple Form Association with Text Field

I have a Rails app that is using the simple_form gem. I have two models that are related, trades and stocks. In the form for trades, I want users to be able to enter their stock ticker symbol in a text field. Currently, I'm using the association function which renders a select box. The problem is that I want a text field instead since I have about a thousand stocks to choose from.
Is there a way I can do this (with or without Simple Form)?
the models:
class Trade < ActiveRecord::Base
belongs_to :stock
end
class Stock < ActiveRecord::Base
has_many :trades
end
the form on trades#new
<%= simple_form_for(#trade) do |f| %>
<%= f.association :stock %>
<% end %>
You should be able to just use this syntax:
<%= f.input :stock_id, :label => 'Enter your ticker:' %>
The problem here is that the user will not know what :stock_id is, as it's a reference to one of your many Stock objects.
So you probably want to implement a simple jquery autocomplete interface that returns a list of stocks like so:
[{:ticker => 'AAPL', :name => 'Apple Inc', :id => 1}, {:ticker => 'IBM', :name => 'International Business Machines', :id => 2}, etc ]
You can then display something like this as autocomplete results:
AAPL - Apple Inc
IBM - International Business Machines
and allow the user to select the one they are looking for. Behind the scenes you capture the :id and use that as your associated :stock_id.
You will need to add a stocks_controller action that takes a string and looks up Stocks based on a partial ticker and returns a max-number of stocks like 20.
def search
ticker_query = "%#{params[:ticker]}%"
stocks = Stock.where('ticker LIKE ?', ticker_query).limit(20)
render :json => stocks
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

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.