Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails select_tag, how to generate dropdown list with json value and custom text from an array

I'ved got a json array and i want to create a dropdown selection list. And because its json, i dont want to display json but based on a readable name.

I tried 2 methods that came the closest, but cant get what i want.

controller:

@books = [{"code"=>"PA1","name"=>"James","type"=>"Novel"},{"code"=>"PA2","name"=>"John","type"=>"Science"}] 

method 1

form.html.erb:

<%= select "book", "book", @books.each_with_index.map {|name, index| [name,name["name"]]} %>

the generated html:

<select id="book_book" name="book[book]"><option code="PA1" name="James" type="Novel" value="James">James</option>
<option code="PA2" name="John" type="Science" value="John">John</option></select></div>

method 2

form.html.erb:

<%= select_tag "book", options_for_select(@books) %> 

the generated html:

<select id="book" name="book"><option value="{&quot;code&quot;=&gt;&quot;PA1&quot;,&quot;name&quot;=&gt;&quot;James&quot;, &quot;type&quot;=&gt;&quot;Novel&quot;}">{&quot;code&quot;=&gt;&quot;PA1&quot;, &quot;name&quot;=&gt;&quot;James&quot;, &quot;type&quot;=&gt;&quot;Novel&quot;}</option><option value="{&quot;code&quot;=&gt;&quot;PA2&quot;, &quot;name&quot;=&gt;&quot;John&quot;, &quot;type&quot;=&gt;&quot;Science&quot;}">{&quot;code&quot;=&gt;&quot;PA2&quot;, &quot;name&quot;=&gt;&quot;John&quot;, &quot;type&quot;=&gt;&quot;Science&quot;}</option></select> </div>   

method 3 (update new method not working as well)

Even this does not work, there is 2 different "value"! Getting more frustrated.

@books = [{"value" => {"code"=>"PA1","name"=>"James","type"=>"Novel"}},{"value" => {"code"=>"PA2","name"=>"John","type"=>"Science"}}] 

<%= select "book", "book", @books.each_with_index.map {|value, index| [value,value["value"]["name"]]} %></div>

<select id="book_book" name="book[book]"><option value="James" value="{&quot;code&quot;=&gt;&quot;PA1&quot;, &quot;name&quot;=&gt;&quot;James&quot;, &quot;type&quot;=&gt;&quot;Novel&quot;}">James</option>
<option value="John" value="{&quot;code&quot;=&gt;&quot;PA2&quot;, &quot;name&quot;=&gt;&quot;John&quot;, &quot;type&quot;=&gt;&quot;Science&quot;}">John</option></select></div>

This is the desired result I want, Please HELP!:

 <select id="book_book" name="book[book]"><option value="{"code"=>"PA1","name"=>"James","type"=>"Novel"}">James</option>
<option value="{"code"=>"PA2","name"=>"John","type"=>"Science"}">John</option></select></div>
like image 676
Axil Avatar asked Mar 10 '14 01:03

Axil


People also ask

How to select selected text from a JSON array in JavaScript?

I’ll first create a JSON array inside a JavaScript function and add few data to it. Next, I’ll iterate (loop) through each value in the array and bind the values to the SELECT element. See how I got the selected text of the SELECT element in the above example.

How to populate a dropdown list with an array in JavaScript?

Here we will populate the dropdown list with an array. Below is the description of popular approaches used in JavaScript. Example 1: In this example, the length property is used to traverse the elements of the array and on each element create an option element and append this new element to the select element by appendChild () method .

How to extract data from a JSON file using JavaScript?

In the first example, I’ll create a JSON array using JavaScript and bind the data to a SELECT element. In the second example, I’ll extract data from an External JSON file using XMLHttpRequest Object, parse the JSON and bind the data to the SELECT element. I’ll first create a JSON array inside a JavaScript function and add few data to it.

How do I get an array of selected items in Rails 3?

To make sure that you’ll receive a array you should declare the name of the select with “ [ ]” like that: An :onchange call can be made using the Rails 3 jquery-ujs helper in the form: The jquery_ujs looks for data-remote and data-url. These can be spelled out or placed in the data hash.


1 Answers

I don't know why you wanna pass whole hash as string in value of your <option> But if you want to generate this

<select id="book_book" name="book[book]"><option value="{"code"=>"PA1","name"=>"James","type"=>"Novel"}">James</option>
<option value="{"code"=>"PA2","name"=>"John","type"=>"Science"}">John</option></select></div>

Then you should write following

<%= select("book", "book", @books.each.map {|hash| [hash['name'], hash.to_s] }) %></div>
like image 84
Hardik Avatar answered Sep 19 '22 21:09

Hardik