Ruby each loop based on array - ruby

I have an array like this:
#airports = [
['Malaysia', 'Alor Setar', 'AOR'],
['Malaysia', 'Bintulu', 'BTU'],
['Malaysia', 'Ipoh', 'IPH'],
['Malaysia', 'Johor Bahru', 'JHB'],
['Indonesia', 'Kuching', 'KCH'],
['Indonesia', 'Labuan', 'LBU'],
['Indonesia', 'Langkawi', 'LGK'],
['Indonesia', 'Miri', 'MYY'],
['Indonesia', 'Penang', 'PEN'],
]
then in my view:
<select name="from" class="form-control select2">
<% #airports.each do |airport| %>
<optgroup label="<%= airport[0] %>">
<option value="<%= airport[2] %>" <%= #params[:from] == airport[2] ? "selected" : "" %>>
<%= "#{airport[1]} (#{airport[2]})" %>
</option>
</optgroup>
<% end %>
</select>
which gives the result like this:
How can I group it for each country? I mean like this:
Malaysia
Alor Setar
Bintulu
Ipoh
Johor
Indonesia
Kuching
Labuan
Langkawi
Penang
Miri

In your view, you can do something like:
<% countries = #airports.group_by{|a| a.first} %>
<% countries.each do |country, airport| %>
<optgroup label="<%= country %>">
<% airport.each do |a| %>
<option value="<%= a[1] %>"></option>
<% end %>
</optgroup>
<% end %>
PS: This is just to give you a rough idea, I'm missing the logic you used for <option value> in my example. Hope you can fix it accordingly.

Related

How to save different values for the checkbox field labels, in ruby on rails?

I have a collection checkboxes in my form, like below.
<% ["cricket" ,"tennis", "not there in list"].each do |c| %>
<div class="col-md-4" >
<p><%= f.check_box :game, {:multiple => true, checked: #training.game&.include?(c), class: "reason-for-the-test"}, c, "" %> <%= c.capitalize.tr("_"," ") %></p>
</div>
<% end %>
Currently the label of the checkbox, and its value that is getting saved in the db, both are same. i want to save different values to the db for the labels. i tries like below. but its not working. can anyone help me with this.
<% [["cricket","cri"] ,["tennis","ten"], ["not there in list","na"]].each do |c| %>
<div class="col-md-4" >
<p><%= f.check_box :game, {:multiple => true, checked: #training.game&.include?(c), class: "reason-for-the-test"}, c, "" %> <%= c.capitalize.tr("_"," ") %></p>
</div>
<% end %>
# ⇓⇓⇓⇓⇓⇓⇓⇓⇓
<% [["cricket","cri"], ["tennis","ten"], ...].each do |name, val| %>
<div class="col-md-4" >
<p><%= f.check_box :game, {:multiple => true, checked: #training.game&.include?(val)}, name, "" %> <%= name %></p>
</div>
<% end %>

Combobox if selected not working

I have a combobox where I submit a value when Onchange event is triggerd. But the 'if' won't work.
The ID and the 'selectedGebouw' are both visible on the screen combobox, but it won't let it as selected.
<select name="gebouwFilter" onchange="this.form.submit()">
<option value="0"></option>
<%
set objRec = objCon.execute(QUERY)
DO WHILE NOT objRec.EOF
%>
<option value="<%=objRec("locationID")%>" <%if selectedGebouw = objRec("locationID") then response.write("Selected") end if %>>
<!-- <%=objRec("address") &", "& objRec("place") %> -->
<%=objRec("locationID") &", "& selectedGebouw %>
</option>
<%
objRec.MoveNext
Loop
objRec.Close
set objRec = nothing
%>
</select>
FIXED:
First changed the submited value to int fixed it:
selectedGebouw = cint(Request.Form("gebouwFilter"))

trying to populate dropdown, error in if else condition ruby

This is the below code that I had written for populating a dropdown.
<html>
<select id = 'status_update' >
<% array = [{"status_name"=>"Submitted", "reachable"=>false, "transition_name"=>""},
{"status_name"=>"Replied", "reachable"=>true, "transition_name"=>"Reply"},
{"status_name"=>"Answered", "reachable"=>false, "transition_name"=>""},
{"status_name"=>"Closed", "reachable"=>false, "transition_name"=>""},
{"status_name"=>"Canceled", "reachable"=>true, "transition_name"=>"Cancel"}]
array.each { |x|
x.each do |key, value|
%>
<option value = "<%= #{x['transition_name']} %>"
disabled = "<%= if ((#{x['reachable']}) == 'false')
return 'disabled'
else
return ''
end %>" ><%= "#{x['status_name']}" %></option>
<% end %>
<% } %>
</select>
</html>
In the above code, array is collection of hashmaps and the keys in hashmaps remains the same, 3 keys with different values in each set. Now, I am trying to populate each hashmap, with the values of their respective keys, in the dropdown. When I try so, I am getting error
ERB syntax error:dropdown:23: syntax error, unexpected kELSE
else
Might be simple, but not able to get the correct way of approach to get the hashmaps in the dropdown. Can anyone of you kindly let me know how to proceed please ?
The second loop in not necessary. This should work:
<% array.each do |x| %>
<option value="<%= x['transition_name'] %>" disabled="<%= x['reachable'] ? '' : 'disabled' %>"><%= x['status_name'] %></option>
<% end %>
Rather than setting disabled="" it's better to omit the attribute:
<option value="<%= x['transition_name'] %>" <%= 'disabled="disabled"' unless x['reachable'] %>"><%= x['status_name'] %></option>
Or, if this is too much in one line:
<% if x['reachable'] %>
<option value="<%= x['transition_name'] %>"><%= x['status_name'] %></option>
<% else %>
<option value="<%= x['transition_name'] %>" disabled="disabled"><%= x['status_name'] %></option>
<% end %>

how to filter an <option> value in ruby for quotes

Have a do loop in HTML supporting a Ruby app:
<% #list.each do |object| %>
<option value="<%= object['name'] %>"><%= object['name'] %></option>
<% end %>
Would like to be able to filter the <option value="<%= object['name'] %>"> item for quotes to preserve the final HTML -- is there an easy way to do this?
If this is a Rails app, then I'd suggest using one of the built-in helpers, like so:
options_for_select(#list.map{ |object| [object['name'], object['name']] })
If not, then maybe just gsub:
<option value="<%= object['name'].gsub("\"","") %>">
In pure Ruby, you can use CGI::escapeHTML, like this:
<% #list.each do |object| %>
<% escaped_value = CGI::escapeHTML(object['name']) %>
<option value="<%= escaped_value %>"><%= escaped_value %></option>
<% end %>
You will have to do a require 'cgi', to use this.
If you are using Rails, you are better off using options_for_select, or use the select_tag or select according to your need, as the other answer specifies

Datamapper update enum value through form

I have a model like this:
class Project
include DataMapper::Resource
property :id, Serial
property :title, String
property :slug, String
property :status, Enum[:open, :closed ], :default => :open
has n, :issues
end
I've created a view to update the project status:
<form action="/project/update" method="post" id="project">
<label for="status">Status
<select id="status">
<option value="0"
<% if(#project.status == :open) %>
selected="selected"
<% end %>
>Open</option>
<option value="1"
<% if(#project.status == :closed) %>
selected="selected"
<% end %>
>Closed</option>
</select>
</label>
</form>
Here's the route:
post '/project/update' do
#project = Project.get(params[:project_id])
#project.update(:title => params[:title])
end
What values does the form need to pass to the route to update the status? and what should the route look like in this instance?
Thanks,
"open" and "closed" - they will be converted to symbols automatically.

Resources