Populate two database values from one dropdown select - ruby-on-rails

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

Related

Rails collection_select, populate drop down field using DB data and arbitrary value

This is more of a curiosity question.
I'm using the the following expression
f.collection_select :location_id, current_provider.locations, :id, :to_s, { include_blank: true}, class: 'form-control'
(in a slim file) to pull up all the location data from the DB, and populate a drop-down menu.
What I'm curious of, is if it's possible to have the DB data in one section of the drop down menu, and an arbitrary default location in another section of the drop down menu.
Looking forward to hear from the community.
Many thanks!
If the arbitrary default location isn't something stored in the locations table in your database, then no, this won't work. This is because location_id is a foreign key column, so the only thing it can store is an id from the locations table. Anything besides that or NULL will cause it to throw an error.
If the location is stored in the locations table, then you should check out the select helper as opposed to collection_select. It lets you manipulate the options. You'd do something like
f.select :location_id, options_manipulated_however, { include_blank: true}, class: 'form-control'

Options select in rails

How I can manually enter the options of a select, I have been using the following:
<%= f.collection_select :establecimiento_id, Establecimiento.order(:nombre), :id, :nombre, include_blank: true %>
But that is used to select data from a table, I want to create one with the months, will it be necessary to create a table with the months or is there a way to enter in the code what I want?
The second param (in your case Establecimiento.order(:nombre)) is the collection for your select. You can pass a array with pairs of values for value and text of that option. For instance, [[1,"January"], [2,"February"] ] and so on will give you the options with the month name, and the value that will be submitted will be the first one of the pair (in that case, the number of month, but could be the name anyway. It depends of what you want to achieve).
Hope it helps. Good luck!
Try:
<%= f.collection_select :establecimiento_id, collection:[[1,"January"], [2,"February"] ], :id, :nombre, include_blank: true %>
I am not sure about the syntax but above answer gave it already away. You can pass options manually. Check API for collection_select:
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select

How to make a collection_select in rails that only displays options with a specific attribute

I'm building an app where a user can customize components in a pc(system), based on components that are available in their respective tables (eg. motherboards table, cpus table.. etc)
I would like the user to be able to select the components via a dropdown select in the system form.
I have been able to achieve this by using collection_select like this
<%= collection_select(:system, :motherboard_id, Motherboard.all, :id, :model, prompt: true) %>
However, the collection_select displays all components in the table, and I wish to only display those components that have an available: true attribute.
I have tried
<%= collection_select(:system, :motherboard_id, Motherboard.any? {|mobo| mobo.available?} , :id, :model, prompt: true) %>
which results in undefined method 'map' for false:FalseClass screenshot:
I thought about adding a before_save callback that checks each items availability, but if that's not the only way to get it to work, I think that would be a poor decision in terms of UX
You can use
<%= collection_select(:system, :motherboard_id, Motherboard.where(available: true), :id, :model, prompt: true) %>

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

Rails Form_For Select With Dual Purpose

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

Resources