My controller passes this array to the view:
#ipd = Ipdw.connection.select_all("select * from [My Preferences]")
This is my view code:
<h1>Hello, Rails</h1>
<body>
<% #ipd.each do |ip| %>
<p><%= ip %></p>
<% end %>
</body>
This is displayed in the browser:
{"Pref ID"=>1, "Source ID"=>1, "Username"=>"RS2559", "Field Name"=>"CATEGORY", "String Value"=>"IP Placemat Deliverables", "Integer Value"=>nil, "Decimal Value"=>nil, "Created Dt Tm"=>2009-08-10 03:01:36 UTC, "Update Dt Tm"=>2009-12-14 16:04:01 UTC}
{"Pref ID"=>2, "Source ID"=>1, "Username"=>"RS2559", "Field Name"=>"TYPE", "String Value"=>nil, "Integer Value"=>nil, "Decimal Value"=>nil, "Created Dt Tm"=>2009-08-10 03:01:40 UTC, "Update Dt Tm"=>2009-12-14 16:04:01 UTC}
...
I want to show the "Username" and "Field Name" in a table. How can I loop through the array to show only these columns in a readable matter?
It looks like your query is returning an array of hashes. Just use the keys to pick the values you are interested in:
<% #ipd.each do |ip| %>
<p>
<%= ip['Username'] %><br />
<%= ip['Field Name'] %>
</p>
<% end %>
How about something like:
<h1>Hello, Rails</h1>
<body>
<% #ipd.each do |ip| %>
<p>
<% ["Username", "Field Name"].each do |k| %>
<%= ip[k] %>
<% end %>
</p>
<% end %>
</body>
That, naturally, will just put the values on your page. You could do other things like build a table or make bullet points or whatever.
That might look something like:
<h1>Hello, Rails</h1>
<body>
<table>
<tr>
<% #field_names.each do |field_name| %>
<th>
<%= field_name %>
</th>
<% end %>
</tr>
<% #ipd.each do |ip| %>
<tr>
<% #field_names.each do |field_name| %>
<td>
<%= ip[field_name] %>
</td>
<% end %>
</tr>
<% end %>
</table>
</body>
This assumes in your controller you did something like:
#field_names = ["Username", "Field Name"]
That way, you can decide in your controller which fields you want (and in what order) and not have to futz with your html.erb file.
If you wanted to pretty up those label names a little, in your controller you could do:
#label_mapping = {"Username"=>"User Name", "Field Name"=>"Field Name"}
And then:
<h1>Hello, Rails</h1>
<body>
<table>
<tr>
<% #field_names.each do |field_name| %>
<th>
<%= label_mapping[field_name] %>
</th>
<% end %>
</tr>
<% #ipd.each do |ip| %>
<tr>
<% #field_names.each do |field_name| %>
<td>
<%= ip[field_name] %>
</td>
<% end %>
</tr>
<% end %>
</table>
</body>
There are maybe 2 ways:
First you have a model for this kind of data:
<% #ipd.each do |ip| %>
<p><%= ip.username %></p>
<% end %>
Secound:
Your Dataset you get from your external Database looks like a hash. So try something like this:
<% #ipd.each do |ip| %>
<p><%= ip["username"] %></p>
<% end %>
I think kind 2 is what you need ;).
In a view i wouldn't use a shorter way of looping, be cause the code get less readable with something like this: abc.each{|letter| "<li>#{letter}</li>"}
Related
I want to show two columns of the code in table data tag"" per row while using '.each' method. But the issue is that the following code displays one column in a row.
<table>
<% #lease.apartment.roommates.each do |roommate| %>
<tr>
<td colspan="5">
<% unless roommate == #lease.second_occupant || roommate == #lease.user %>
<% if roommate.current_room.present? %>
<p>
<%= roommate.full_name %> -
<% if roommate.current_room.apartment == #lease.apartment%>
<%= roommate.current_room&.label %>
<% end %>
<br>Email:<%= roommate.email %><br>Phone:<%= roommate.phone %><br>
<% if #lease.end_at.present? %>
Lease End date (if applicable):<%= #lease.end_at %>
<% end %>
</p>
<% end %>
<% end %>
</td>
</tr>
<% end %>
</table>
You can use the each_slice method for dividing the array into slices of 2 elements each:
<table>
<% #roommates.each_slice(2) do |roommate_slice| %>
<tr>
<td colspan="5">
<%= roommate_slice[0].full_name %>
<%= roommate_slice[1].full_name %>
</td>
</tr>
<% end %>
</table>
I am trying to print data from a object "root" to a table, everythink work fine but not printing this name i dont know why.
Code :
<% root = NameOfDatabase.find_by_d_id(d.id) %>
<% logger.info root.inspect %>
<% if root.nil? %>
<td class="col-4"> <% "-" %> </td>
<% else %>
<td class="col-4"> <% root.name %> </td>
<% end %>
Picture: View
Log file :
...
#<Name**** id: ****, d_id: ****, name: "Alice", dd_id: ****>
Completed in 99ms (View: 7, DB: 33) | 200 OK [https://*********/dd/search_ajax?option=active&query=s001]
As following in root we have data i want only print root.name in table !!
thanks you
You need to use <%= %> instead of <% %>
<% %> will just evaluate the expression, it won't output the result to ERB template
<%= %> will evaluate the expression and outputs to ERB template.
Try the below:
<% root = NameOfDatabase.find_by_student_id(student.id) %>
<% logger.info root.inspect %>
<% if root.nil? %>
<td class="col-4"> <%= "-" %> </td>
<% else %>
<td class="col-4"> <%= root.name %> </td>
<% end %>
Reference:
What is the difference between <%, <%=, <%# and -%> in ERB in Rails?
I am creating a calendar in Rails using simple_calendar.
enter image description here
However, the date of the calendar appears in the image as 2018-08-23.
I would like to display the date on the calendar day by day like 23 instead of 2018-08-23.
_month_calendar.html.erb
<div class="simple-calendar">
<div class="calendar-heading">
<%= link_to t('simple_calendar.previous', default: 'Previous'), calendar.url_for_previous_view %>
<span class="calendar-title"><%= t('date.month_names')[start_date.month] %> <%= start_date.year %></span>
<%= link_to t('simple_calendar.next', default: 'Next'), calendar.url_for_next_view %>
</div>
<table class="table table-striped">
<thead>
<tr>
<% date_range.slice(0, 7).each do |day| %>
<th><%= t('date.abbr_day_names')[day.wday] %></th>
<% end %>
</tr>
</thead>
<tbody>
<% date_range.each_slice(7) do |week| %>
<tr>
<% week.each do |day| %>
<%= content_tag :td, class: calendar.td_classes_for(day) do %>
<% if defined?(Haml) && respond_to?(:block_is_haml?) && block_is_haml?(block) %>
<% capture_haml(day, sorted_events.fetch(day, []), &block) %>
<% else %>
<% block.call day, sorted_events.fetch(day, []) %>
<% end %>
<% end %>
<% end %>
</tr>
<% end %>
</tbody>
</table>
</div>
According to the documentation, you have to call day on the date object.
I assume that in your case, the date object is the one named day, so you have to change 2 lines in your view (those inside if/else statement):
<% if defined?(Haml) && respond_to?(:block_is_haml?) && block_is_haml?(block) %>
<% capture_haml(day.day, sorted_events.fetch(day, []), &block) %>
<% else %>
<% block.call day.day, sorted_events.fetch(day, []) %>
<% end %>
I wonder if/how to change the name of the param :keyword when using acts as taggable?
Today my url looks like this:
http://www.foo.bar/tagged?keyword=baz
I would like to change the "keyword" to another word.
controller
def tagged
#current_keyword = params[:keyword]
#tags = FeedEntry.tag_counts_on(:keyword)
#tagged_feed_entries = FeedEntry.tagged_with(params[:keyword]).order("published_at desc").paginate :page => params[:sida]
end
View:
<table class="table table-striped">
<tbody>
<% if #tags %>
<% #tagged_feed_entries.each do |feed_entry| %>
<tr>
<td>
<% today = Date.today %>
<% if (today === feed_entry.published_at.to_date) %>
<span class="label label-success">
<% else %>
<span class="label">
<% end %>
<%= format_stamp_to_date(feed_entry.published_at) %>
kl:
<%= I18n.localize(feed_entry.published_at, :format => '%H:%M') %>
</span>
</td>
<td><%= link_to feed_entry.name, feed_entry_path(feed_entry) %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
<%= will_paginate #tagged_feed_entries, :param_name => :sida %>
You should just be able to replace all instances of params[:keyword] to params[:whatever]. Your path then becomes http://www.foo.bar/tagged?whatever=baz.
If you have a search form, you'll have to make the relevant changes there, as well.
In my rails application I have a set of attachments that display, with a main image being one of them. However, I would like to loop through all of them except the main attachment. My view code right now is:
<% if #asset.attachments.size > 0 %>
<table>
<tr>
<% #asset.attachments.each_with_index do |attachment, i| %>
<% if i%5 == 0 %>
</tr><tr>
<%end%>
<td style="width: 150px;" valign="bottom" align="center">
<%= image_tag(attachment.attachment.url(:thumb)) if attachment.is_image? %>
<%= image_tag("/images/Excel.png") if attachment.is_excel? %>
<%= image_tag("/images/Word.png") if attachment.is_word?%>
<br />
<%= link_to attachment.name.to_s,attachment.attachment.to_s %>
</td>
<%end%>
</tr>
</table>
<%end%>
but, I would like to add something like if !main_image to this line of code:
<% #asset.attachments.each_with_index do |attachment, i| %>
I don't know if that is possible though.
This answer is in continuation to answer on this question
Add another method to your model, which return you the attachments which are not main image.
scope :not_main_image, where(:main_image => false)
On a side note, you might want to move all this logic into a helper method:
<%= image_tag(attachment.attachment.url(:thumb)) if attachment.is_image? %>
<%= image_tag("/images/Excel.png") if attachment.is_excel? %>
<%= image_tag("/images/Word.png") if attachment.is_word?%>
<br />
<%= link_to attachment.name.to_s,attachment.attachment.to_s>
Say you create a helper method like:
def link_to_attachment attachment
html = ""
html += image_tag(attachment.attachment.url(:thumb)) if attachment.is_image?
html += image_tag("/images/Excel.png") if attachment.is_excel?
html += image_tag("/images/Word.png") if attachment.is_word?
html += "<br />"
html += link_to(attachment.name.to_s, attachment.attachment.to_s)
html.html_safe
end
Then in you view you can replace it with:
<td style="width: 150px;" valign="bottom" align="center">
<%= link_to_attachment attachment %>
</td>
<% #asset.attachments.each_with_index do |attachment, i| %>
<% next if attachment.main_image %>
It depends on how you define the main attachment
<% #asset.attachments.each_index do |attachment, i| %>
<% unless attachment.main? %>
...
<% end %>
<% end %>
OR
<% #asset.attachments.each_index do |attachment, i| %>
<% if attachment.main? %>
<% next %>
<% else %>
...
<% end %>
<% end %>
OR put this in your controller
#attachments = #asset.attachments.where(:main => false)
after that, iterate over #attachments instead of #asset.attachments