Ruby on rails - bind a selected value to a select element for update - ruby-on-rails

I can not figure out why this is not working, I saw a couple of examples and seems to be right.
I have two clases that are related, consultant and salary (a salary belongs_to a consultant).
The problem I have is, when I want to edit a salary, the consultant that appears on the form is not bind to the select (in the select it just appears the list as if I was creating a new one)
<%= f.select :consultant_id, options_for_select(Consultant.active.map{|s|[s.first_name, s.id]}, params[:consultant_id]), {}, class: 'form-control' %>

I think you should check your route that the :consultant_id param is available on this page. Else you may need to change the params[:consultant_id] to something like salary.consultant_id
Alternatively, you can use the collection_select method as such:
f.collection_select(:consultant_id, Consultant.active, :id, :first_name, prompt: true, class: 'form-control')
Let me know whichever works for you.
Note: It's not best practice referring to domain object within your view.

Related

rails - getting a pair of values from select_tag and option_groups_from_collection optgroup

Is it possible to make select_tag return a pair of values (group_id, item_id) using option_groups_from_collection_for_select?
= select_tag :item_id,
option_groups_from_collection_for_select(#groups, :items, :name, :id, :proper_name),
class: 'form-control'
Each group has many items association, each item can belong to one or more groups.
I'd like to make the helper to add group_id to optgroup id attribute in addition to adding item_id to each item option and fetch those two on selection to send them to a controller, but I can't figure out how, nor is it even possible (or proper..).
I could make two different dropdowns, where 2. one would be loaded using ajax after selecting an option in the 1. one, but this just doesn't seem right..
EDIT
I've figured that I can pass a proc as a value_method, but unfortunately I'm unable to fetch group_id in there..
= select_tag :item_id,
option_groups_from_collection_for_select(#groups, :items, :name, ->(el) { "#{el.id}" }, :proper_name),
class: 'form-control'
Kind regards!
EDIT
Ok, I've finally figured it out, thanks to https://stackoverflow.com/a/6374301/19174916 :)
Ok, I've finally figured it out, thanks to https://stackoverflow.com/a/6374301/19174916 :)
Used grouped_options_for_select and passed group_id as a data attribute to each option, which I'd pass along with url later using simple JS.

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

select_tag doesn't save the selected option

I'm new in web development & Rails. I've been struggling to understand why my form is not getting saved completely. Here is the code I'm using:
<div class="field">
<%= f.label :type %><br>
<%= select_tag(:type, options_for_select([['Verb', 'Verb'], ['Adjective', 'Adjective'], ['Noun','Noun'],['Preposition','Preposition'],['Article','Article'],['Adverb','Adverb']])) %>
</div>
<div class="field">
<%= f.label :category %><br>
<%= select_tag "category",
"<option>Appliances</option>
<option>Clothes and Accessories</option>
<option>Colours</option>
<option>Communication and Technology</option>
<option>Documents and Texts</option>
<option>Education</option>
<option>Entertainment and Media</option>
<option>Family and Friends</option>
<option>Food and Drink</option>
<option>Health, Medicine and Exercise</option>
<option>Hobbies and Leisure</option>
<option>House and Home</option>
<option>Measurements</option>
<option>Personal Feelings, Opinions and Experiences (adjectives)</option>
<option>Places: Buildings</option>
<option>Places: Countryside</option>
<option>Places: Town and City</option>
<option>Services</option>
<option>Shopping</option>
<option>Sport</option>
<option>The Natural World</option>
<option>Time</option>
<option>Travel and Transport</option>
<option>Weather</option>
<option>Work and Jobs</option>".html_safe %>
</div>
PS: I've kept two different methods I tried to use.
use f.select instead of select_tag.
f.select(:type, [['Verb', 'Verb'], ['Adjective', 'Adjective'], ['Noun','Noun'],['Preposition','Preposition'],['Article','Article'],['Adverb','Adverb'])
or if you are using form_for and passing an object then you can also do it as follows.
select_tag(:type, options_for_select([['Verb', 'Verb'], ['Adjective', 'Adjective'], ['Noun','Noun'],['Preposition','Preposition'],['Article','Article'],['Adverb','Adverb']],f.object.type))
we are passing a value of type from actual object into option for select.
As you are using option_for_select it expects that you send selected value as a second parameter.
options_for_select also takes second parameter which is the selected value.
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_for_select
# this will show Preposition selected
options_for_select([['Verb', 'Verb'], ['Adjective', 'Adjective'], ['Noun','Noun'],['Preposition','Preposition'],['Article','Article'],['Adverb','Adverb']], 'Preposition')
For future reference, please always specify Rails version while posting question.
I noticed you are using f.label, in which case you might also want to take a look at http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select
HTH
For the first one, you should use f.select instead of select_tag, rails tags are helpers for generate html elements, but in this case you need an item that is linked to your form, so you use the form helper for select instead.
For the other example, i'm not entearly sure if it will work like that, but try with the same idea, you should found that the select is passed to your controllers, also use the symbol name instead the string name, meaning :category instead "category", if you want to have a phrase like "select a category...." add another option at the end with :prompt => "select a category...", hope it helps and look at Ryan Bates site, is an excellent place to learn rails

Resources