Rails Form_For Select With Dual Purpose - ruby-on-rails

I have form for adding a new job. On my form I have a select drop-down list. I need to associate the new job to a customer. The following works great.
<%= f.collection_select :customer_id, Customer.all, :id, :business_name %>
But, what if I want to also be able to send in a customer_id to the new form? Can I have the form's select drop-down show all the possible customers, as above, but have it auto select the customer_id I pass into the form, if a customer_id is passed in?
url = ...jobs/new
OR
url = ...jobs/new?customer_id=5
I apologize if I did not explain this well enough.
Thanks in advance.
--jc

I think you do what you're trying to achieve this by populating the customer_id field on the job you're creating in your controller if customer_id is present in the request params. This should make that particular customer be the initially selected option in the form.
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
e.g. Something like.
if params[:customer_id].present?
job.customer_id = params[:customer_id]
end

If you declaring the instance variable #customer in your controller action then you can use selected option as:
<%= f.collection_select :customer_id, Customer.all, :id, :business_name, {:selected => #customer.id} %>

Related

Populate two database values from one dropdown select

I have a dropdown menu:
<%= f.collection_select :price, Print.all, :printprice, :sizetotal, {prompt: "Pick A Size & Medium"} %>
It currently populates the :price database column with the value :printprice, and shows the value :sizetotal in the actual dropdown selection.
I want to be able to populate two database columns, one of which is the :price column which is already working, but I also want to populate another column called :size with the value :printsize. I want to do this using the same dropdown select menu.
Something like:
<%= f.collection_select :price, :size, Print.all, :printprice, :printsize, :sizetotal, {prompt: "Pick A Size & Medium"} %>
But obviously the above doesnt work
Is this possible?
Following my comment, here is the ID alternative. Basically you want to send only the ID of the print, and find again the whole object on the backend during the #create or #update actions
print_for_size_and_price = Print.find(params[:print_id_for_size_and_price])
So on the frontend I'd use something like this
select_tag(
:print_id_for_size_and_price,
options_from_collection_for_select(Print.all, :id, :sizetotal)
)
And in the backend I'd do somewhere
my_instance.price = print_for_size_and_price.printprice
my_instance.size = print_for_size_and_price.sizetotal

options_from_collection_for_select in rails 4

I'm building a simple form to let users of my rails 4 site message each other.
In the form, the user can select the recipient of the message with a select list of all the users in the system (it's a small user base).
Right now the form control looks like this, and works fine:
<%= f.select(:to_id, options_from_collection_for_select(User.all, :id, :first_name), {}, {:class => 'form-control'}) %>
However, there's a "nickname" field in the Users table as well-- I'd like to have the form show the nickname (if it exists) as well as the user's first / last name-- so the value of the select would be:
<option>Snoop Dogg - Calvin Broadus</option>
rather than just
<option>Calvin</option>
Is this possible?
Make a display_name on the Users model
def display_name
nickname || first_name
end
Then update your form to reference it
<%= f.select(:to_id, options_from_collection_for_select(User.all, :id, :display_name), {}, {:class => 'form-control'}) %>

Rails: how to get fields of rejected submit form?

I got one big form with many fields. There are multiple select_tag fields:
-(0..2).to_a.each do |id|
= select_tag('product[category_ids][]', options_from_collection_for_select(Category.all, :id, :name), :prompt=> '-- Select a Category --')
Problem: If the form is rejected upon submit, the :new action does not remember what is the previous selected values of select_tags.
Every other field reappears after submit (like :title, :description), but categories_id are lost.
Probable solution: We must add default selection to the select_tag in the options_from_collection_for_select method. How to get that category_id value? How can we access the fields of the previous form?
options_from_collection_for_select(Category.ordered, :id, :name, category_id)
You could store this data in the session and load it in the new action if it exists and clear that from the session.
That way anytime the fields reject the one that is not remembered loads into the session and is loaded into the select on the new action. Otherwise nothing is loaded when this session variable is blank.
Hope this helps.
In my form
#product.categories = []
but
#product.category_ids = [1,42,57]
So I added one messy if statement:
-if #product.category_ids.count == 0 #create new product
-(0..2).to_a.each do |id|
= select_tag('product[category_ids][]', options_from_collection_for_select(Category.all, :id, :name), :prompt=> '-- Select a Category --')
-else #edit old product, or fixing errors to previous submit
-#product.category_ids.each do |category_id|
= select_tag('product[category_ids][]', options_from_collection_for_select(Category.all, :id, :name, category_id), :prompt=> '-- Select a Category --')

Form_for select field, concatenating data into the display string

I am trying to use a form_for collection_select to display some select field options of account types.
Its occurred to me that it would be easier for the user if they could see the price of the type in each select option
this is my currently not-working code:
<%= a.collection_select :account_type, AccountType.all, :id, (:name+" - "+number_to_currency(:price)) %>
how can i concatenate the values so that (:name+" - "+number_to_currency(:price)) will actually work and not throw an error?
See the documentation:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
You can use the :text_method option to set the displayed text in the select dropdown.
In your AccountType model, define a method like this:
def name_with_price
"#{name} - $#{price}"
end
Then, in your view, you can use:
<%= a.collection_select :account_type, nil, AccountType.all, :id, :name_with_price %>

Basic rails collection select question

A card can have one of many card_types. There are two models, card and card_type, where card_type is an [id, card_type_desc] pairing.
When you define a new card, you have to pick a card-type from a drop down list.
I have the list rendering correctly with the below collection_select box, but the new card.card_type_id field is NULL. How do you set it to the value from the list?
<%= collection_select(:card_type, :id, #card_types, :id, :card_type_desc) %>
Thanks in advance.
I am guessing, It should be...
<%= collection_select(:card, :card_type_id, #card_types, :id, :card_type_desc) %>

Resources