Dynamically populate select dropdown from parameters - ruby-on-rails

How would I populate a select dropdown in my view to be the value of a certain parameter:
http://localhost:3000/?group_size=6 - where I want the dropdown selects to be 1-6 as so:
<select name="group_size">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
<option value="6">Six</option>
</select>
Best would be to display the text for the number, but at least to create the text fields without names would be needed. This is Ruby 2.3 Rails 4.2. What do I need to write in the view and/or controller to make this happen?

In your view you could do something like...
<select name="group_size">
<% (1..params[:group_size]).each_with_index do |i| %>
<option value="<%= i %>"><%= i %></option>
<% end %>
</select>
This creates a Range between 1 and whatever group size is passed. See the Ruby documentation for each_with_index here.
Note: This will break if no group_size is provided. I'll leave it to you to guard against nil. :)

Related

Adding the values of my selected option into the query params of my URL fetch call

I am wondering what code would work in order to get what the user selects and insert it into my line of fetch URL so it changes the URL and can append the relevent information. Using my API.
<body>
<label>
<select class="browser-default">
<option value="" disabled selected>Choose your option</option>
<option value="28">Action</option>
<option value="35">Comedy</option>
<option value="18">Drama</option>
<option value="27">Horror</option>
<option value="99">Documentary</option>
</select>
</label>
</body>
This is the HTML and the URL I wanted to change is
fetch('https://streaming-availability.p.rapidapi.com/search/basic?country=us&service=netflix&type=movie&genre=*28* &page=1&output_language=en&language=en', options)
I would like the value of 28, after the words genre= to change to the value of the option selected.

Rails: generate select box for form

I want to make a select box for a collection. I use method options_for_select but It just generates <option></option> field, not <select></select> outside.
Here is my code:
<% categories_array = Category.all.map{|category| [category.name, category.id]} %>
<%= options_for_select(categories_array) %>
And here is the result:
<option value="5483c910485559047a000000">Programming</option>
<option value="5483c921485559047a010000">Business</option>
<option value="5483c92b485559047a020000">Game Programming</option>
But I expected:
<select id = "categoryId">
<option value="5483c910485559047a000000">Programming</option>
<option value="5483c921485559047a010000">Business</option>
<option value="5483c92b485559047a020000">Game Programming</option>
</select>
Moreover, I can get this value when publish this form to server. How can I do this
Thanks :)
You can see how select and options helpers are combined here:
http://guides.rubyonrails.org/form_helpers.html#making-select-boxes-with-ease
As noted there:
<%= select_tag(:city_id, options_for_select(...)) %>

add hash tag to URL on dropdown item select

Front end I'm using Backbone and back-end rails.
I have a dropdown list like below
<select id="data-source-dropdown">
<option value="1" <#if(client_type==1){#>selected<#}#>><a href='#adform' >Ad forms</a></option>
<option value="2" <#if(client_type==2){#>selected<#}#>><a href='#ansform' >Ans form</a></option>
<option value="3" <#if(client_type==3){#>selected<#}#>><a href='#bform' >B Form</a></option>
</select
When i select one of them, i want to display in URL like if ad form selects i need localhost:3000#adform
which is not displaying using above code
How to do that for drop downs
Is there a way to do it in backbone or rails
I just experimented if I can do and it worked, try it.
<select id="data-source-dropdown" onchange="append_info()">
<option value="">Select one--</option>
<option value="#adform">Ad forms</option>
<option value="#ansform">Ans form</option>
<option value="#bform"> B Form</option>
</select>
I'm not sure of your rails condition, use <% %>
<script type="text/javascript" >
function append_info() {
var val = document.getElementById("data-source-dropdown").value;
window.history.pushState("", "", window.location.href + val);
}
</script>
On change url keeps appending to the previous ..
use rails condition like this for selected
<option value="#adform" <% if condition %> selected <% end %> >Ad forms</option>

Disable html styles for select_date tag in Rails

Whats the best way to disable additional html generation for date helpers in Rails?
Lets say i have a field on form <%= select_month(...) %> and it generates div class and new span inside. Is there a way to generate just the select html tag?
Of course i can generate the whole tag by myself, but just wanted to know if thats possible.
Thanks
select_month() doesn't generate any other tags beside the select tags.
For example <%= select_month Date.today %> will generate
<select name="date[month]" id="date_month">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10" selected="selected">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
Could you post the code which generates div and span tags?

Help with rails collection select

I need to add different values for each option tag in my collection_select cause Im trying to use this jquery plugin.... How do I do that?
Heres my collection select code
<%= e.collection_select(:id,State.all,:id,:name) %>
The output should be something like
<select name="state[id]" id="state_id" class="selectable">
<option value="">-- select --</option>
<option value="1" title="florida">Florida</option>
<option value="2" title="georgia">Georgia</option>
</select>
Please help.
Hmm, I believe you gotta write your own helper to do so.

Resources