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 %>
Related
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 %>
In Rails, there exist such helper as "concat" to output variables inside <% %> block for erb remplates. Which helper can I use in Sinatra to perform the same action, without using <%= %> block ? I mean, something like
<%
#code
concat "This should be rendered in HTML, not in console"
#code
%>
EDIT
The code in the view is something like this (yep, too much logic, but this is my first app in Ruby):
<% Dir.glob('uploaded/*').select do |entry| %>
<div class="singleFileItem">
<% if File.directory? entry %>
<img src="images/folder.png">
<% else
case entry.to_s.split(".")[1]
when "doc","docx" %>
<img class="pictogram" src='images/word.png'>
<% when "xls","xlsx" %>
<img class="pictogram" src='images/excel.png'>
<% when "pdf" %>
<img class="pictogram" src='images/pdf.png'>
<% when "png", "jpg", "jpeg" %>
<img class="pictogram" class="imageRaw" src="<%= entry.to_s %>">
<% else %>
<% end
end %>
<br>
<span class="subFileText">
<%= entry.to_s.split("/")[1][0..14] %>...
</span>
</div>
<% end %>
Thanks, guys, I have finally found it.
I have extended the app.rb file with
set :erb, :outvar => '#output_buffer'
def concat(text)
#output_buffer << text
end
And it works. Just type in the .erb view
<% concat "Text that should be added to render" %>
And you all done. Hope, this will help to someone with similar question
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.
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
I am trying to execute a flash partial like the following:
<% flash.each do |key, value| %>
<div class="flash <%= "#{key}" %>"><%= "#{value}" %></div>
<% end %>
And am getting the following error:
undefined method `safe_concat' for []:Array
The partial is being updated via an ajax request as follows:
page.replace_html "flash_messages", :partial => 'layouts/flash', :locals => { :flash => flash }
Any ideas? I have never seen something like this before.
Thanks.
Shouldn't it be the following?
<% flash.each do |key, value| %>
<div class="flash #{key}"><%= value %></div>
<% end %>
There's no need to wrap the key and value into "#{}". I suppose the nested <> is what made it choke on your line.
I can't try so I wouldn't know if it actually can access key outside of <% %>, so maybe you would need something like:
<% flash.each do |key, value| %>
<% class_names = "flash #{key}" %>
<div class="<%= class_names %>"><%= value %></div>
<% end %>