options_from_collection_for_select doesnt accept hash as argument - ruby-on-rails

I have a select_tag with an options_from_collection_for_select that looks like this :
<%= select_tag "dash_select", options_from_collection_for_select(session[:user_hotel_list], "id", "name") %>
My variable session[:user_hotel_list] is a collection of hashes that looks something like this :
[{"id"=>31, "name"=>"Plaze", "contact_mail"=>"plaze#gmail.com", "contact_phone"=>"+33666027414", "address"=>"JDJDJ", "postcode"=>"92200", "city"=>"JDJDJ", "nb_room"=>nil, "created_at"=>"2016-02-26T12:12:39.214Z", "updated_at"=>"2016-02-26T12:12:39.214Z", "auditor_id"=>nil, "account_id"=>48}, {"id"=>30, "name"=>"dndjd", "contact_mail"=>"djdj#gmail.com", "contact_phone"=>"+33666027414", "address"=>"k", "postcode"=>"92200", "city"=>"kk", "nb_room"=>nil, "created_at"=>"2016-02-25T22:27:47.035Z", "updated_at"=>"2016-02-25T22:27:47.035Z", "auditor_id"=>nil, "account_id"=>48}]
Unfortunately I get undefined methodname' for #`
How can I convert my object to a structure that is accepted by rails options_from_collection_for_select?

You can use options_for_select and #collect to make this a bit easier.
<%= select_tag "dash_select", options_for_select(session[:user_hotel_list].collect{|h| [h['name'], h['id']]}) %>
That should get you what you are looking for and hopefully save you some time!
Answer was taken from How to populate a select_tag with an array of hashes?.

Related

Uniq value in option_from_collection_for_select

How i can print only unique value in options_from_collection_for_select() helper ?
Because i don't want to have 10 times the same value...
<%= select_tag :type, options_from_collection_for_select(
Course.where(category_id: #category.id),
:learning_type,
:learning_type,
#type)
%>
I'm not sure what you are trying to do here. Let's say you have Course model, and you want to show the uniq learning_type of the course. You can get the results as an array with something like:
Course.where(category_id: #category.id).pluck(:learning_type).uniq
or
Course.where(category_id: #category.id).pluck('DISTINCT learning_type')
It's always better to use Fat model and use scopes in your Course model thought.
Now you can easily use the array with that form helper, something like:
<%= select_tag :type, options_for_select(Course.where(category_id: #category.id).pluck('DISTINCT learning_type')) %>

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

Ruby on Rails: combobox is showing id and not the name

I got the code
<%= f.select :wahl1, options_for_select(#berufs) %>
and im getting id´s or smth #<Beruf:0x45ed8e8> instead of the name of #berufs in the combobox
options_for_select expects a simple array or hash of keys and values. Yet you are passing it a collection of models.
What you want is options_from_collection_for_select, for example:
= f.select :wahl1, options_from_collection_for_select(#berufs, 'id', 'name')

Array as Parameter from Rails Select Helper

I'm working on a legacy project that is using acts_as_taggable_on which expects tags to come in arrays. I have a select box allowing users to select a tag on a Course in a field called categories. The only way mass assignment create will work is if params looks like this params = {:course => {:categories => ['Presentation']}}. I've currently a view with this helper:
<%= f.select 'categories', ['Presentation' , 'Round Table' , 'Demo', 'Hands-on'] %>
Which will give me a parameter like params = {:course => {:categories => 'Presentation'}}. This doesn't work since Acts as tag gable apparently can't handle being passed anything other than a collection.
I've tried changing categories to categories[] but then I get this error:
undefined method `categories[]' for #<Course:0x007f9d95c5b810>
Does anyone know the correct way to format my select tag to return an array to the controller? I'm using Rails 3.2.3
I didn't work with acts_as_taggable_on, but maybe this simple hack will be suitable for you? You should put it before mass-assignment.
category = params[:course][:categories]
params[:course][:categories] = [category]
If you are only going to allow the selection of ONE tag, you could do:
<%= f.select 'categories', [['Presentation'] , ['Round Table'] , ['Demo'], ['Hands-on']] %>
Each one item array will have first for the display value, and last for the return value, which in this case will both return the same thing, as the first element of the array is the same as the last element when the array as one element.
Seems like select doesn't give you that option.
If I understand correctly, one option might be to use a select_tag instead and just be explicit about where you want the selection in the params:
<%= select_tag 'course[categories][]', options_for_select(['Presentation' , 'Round Table' , 'Demo', 'Hands-on']) %>
That ought to get your params the way you need them.
Here's what I'm using for one of my projects:
<% options = { include_blank: true } %>
<% html_options = { required: true, name: "#{f.object_name}[#{resource.id}][days][]" } %>
<%= f.select :days, DAYS, options, html_options %>
Without html_options[:name], Rails handles the name of the select tag and spits out something like
service[service_add_ons_attributes][11][days]
but I need
service[service_add_ons_attributes][11][days][]
So I override it.
Hope that helps.

Use helper select or select_tag with JSON on Rails 3.2.1

How can I use a json with select or select_tag in Rails ?
In my controller I make a call to some url and retrieve some data in json format (only two columns, :id :name)
In my view file, I want to use the helper tag select or select_tag to use this json to fill it, but I'm having errors or wrong data
thanks
[{"id":"197630", "name":"JOHN PERS"} , {"id":"6", "name":"JOSHUA JOSH"}]
also I've tried to decode the json with this code
#parsed_json = ActiveSupport::JSON.decode(#listJson)
but it's not working
Well, if #list_json is a real json we can do this:
<% collection = options_for_select(ActiveSupport::JSON.decode(#list_json).map { |b| [b["name"], b["id"]] }) %>
<%= f.select :name, collection %>

Resources