Print hash on table html - Ruby on rails - ruby-on-rails

I have an instance variable #report_antennas_control_access with this data
[{:id_control_access=>1, :input=>"Antena 1"}, {:id_control_access=>1, :output=>"Antena 2"}, {:id_control_access=>1, :input=>"Antena 5"}, {:id_control_access=>2, :input=>"Antena 3"}, {:id_control_access=>2, :output=>"Antena 4"}] and I want to print it in my .html table but in doing so I do it in a way that I do not want
I do it this way:
<tbody>
<% #report_antennas_control_access.each do | antennas | %>
<tr>
<% if control_access[:id_control_access] == antennas[:id_control_access] %>
<td><%= antennas[:input] %></td>
<td><%= antennas[:output] %></td>
<% end %>
</tr>
<% end %>
</tbody>
But he prints it to me in a way I do not want:
This is the way I need to print that data (example):

As David already said, with your input, it will be really difficult to achieve what you need. Since it was fun, I fixed it, but I do believe that it should be fixed somewhere higher in your code (I hope you're using Ruby 2.5+, if not, let me know which version you are on).
def fix_my_data(data)
data.group_by { |x| x[:id_control_access] }
.transform_values do |v|
v.map { |h| h.slice(:input, :output) }
.group_by { |h| h.keys.first }.values.inject(:zip).map { |x,y| x.merge(y.to_h) }
end
end
If you pass your array into this function, it will return this:
{1=>[{:input=>"Antena 1", :output=>"Antena 2"}, {:input=>"Antena 5"}],
2=>[{:input=>"Antena 3", :output=>"Antena 4"}]}
Which should be really simple to generate HTML with, like so:
<tr>
<% #data[control_access[:id_control_access]].each do |antenna| %>
<td><%= antenna[:input] %></td>
<td><%= antenna[:output] %></td>
<% end %>
</tr>
I'm pretty sure fix_my_data can be written in a bit simpler way, but as I mentioned, it's a late place to be fixing the data.

The problem is that you are iterating over each hash and trying to access data in either the hash before or after. Take the first hash for example: {:id_control_access=>1, :input=>"Antena 1"}. You call antennas[:input] on it, so it displays "Antena 1". But then you call antennas[:output], and there is no output key in the current hash, so it's returning nil and causing the corresponding table cell to be blank.
You should consider updating the structure of your hashes, if you can, so that they look like {:id_control_access=>1, :input=>"Antena 1", :output=>"Antena 2"}. It seems to me to make more logical sense, and would solve the problem with your table.

Related

How to present JSON information in a html.erb file (Ruby)?

I am new to Ruby/Rails and I've been given a task to make a Ruby site (.html.erb) look a bit nicer. One of the things requested was to present information on the site, which is currently shown as JSON, as nice-looking html. The line of html.erb is as follows:
<%= #buyer.generate_profile.inspect %><br>
and will display the information it receives on the site as JSON. What can I do to parse through the JSON and make it so that the site will display the information as proper html?
You might want to check out JSON.pretty_generate. You could display the result in a code block like:
<%# If your data is a JSON string, convert it to a hash %>
<% hash = JSON.parse(#buyer.generate_profile) %>
<pre>
<code>
<%= JSON.pretty_generate(hash) %>
</code>
</pre>
So the generate_profile method returns a Hash object and calling inspect on it will create string output of the Hash syntax in Ruby, so the result looks like your comment.
If you wanted it to look like JSON, the simplest way would be to use the to_json method like this
<%= #buyer.generate_profile.to_json %>
but honestly, it's not much better. And even if you were to look into methods that can present it with proper line breaks, etc. that wouldn't improve it much either imo.
I would recommend you learn how to iterate over the keys and values in a Hash, because that would allow you to create a custom HTML layout that could look however you wanted it to.
As an example, I'll show you how you could create a simple table based on the data from your comment
<table>
<thead>
<tr>
<th>Key</th>
<th>XX</th>
<th>YY</th>
<th>ZZ</th>
</tr>
</thead>
<tbody>
<% #buyer.generate_profile.each_pair do |key, sub_hash| %>
<tr>
<td><%= key %></td>
<td><%= sub_hash[:xx] %></td>
<td><%= sub_hash[:yy] %></td>
<td><%= sub_hash[:zz] %></td>
</tr>
<% end %>
</tbody>
</table>
The main thing here is the use of each_pair which is a method that iterates over each key and value of the hash. In your case it sounds like the values of the first hash are sub-hashes, hence my use of sub_hash as the block argument.
You could technically use each_pair on the sub-hashes as well, but I'm guessing they all have the same keys, that's why I manually added columns and cells for XX, YY, ZZ in combination with sub_hash[:xx], etc.
Once you get a hang of how iterating over Hashes and arrays work, then I would recommend scrapping the table design and instead look into more modern web design approaches like Flex and CSS-Grid, but one thing at a time :)

How to avoid hitting database in the view

I get that one should not ping the database in the view... but wondering about the right solution. In one of my views, I need to pull info on an #order, it's child items, and also Amount, another model, based on each child item. Something like this:
<% #order.items.each do |item| %>
<td><%= item.name %></td>
<td><%= Refund.where(item_id:item.id).first.amount %></td>
<td><%= Amount.where(item_id: item.id).first.amount %></td>
<% end %>
For the sake of avoiding the db hits in the view, the only solution I've thought of is to create a huge hash of all the relevant data in the controller, which is then accessed from the view. So it would be something like this:
# controller (writing quickly, code may not be totally right, hopefully you get gist
data = Hash.new
data["items"] = []
#order.items.each do |item|
item_hash = {
"name" => item.name,
"amount" => Amount.where(item_id: item.id).first.amount,
"refund" => Refund.where(item_id:item.id).first.amount
}
data["items"] << item_hash
end
# view code
<% data["items"].each do |item| %>
<td><%= item["name"] %></td>
<td><%= item["refund"] %></td>
<td><%= item["amount"] %></td>
<% end %>
And I know SO hates this type of question... but I really need to know... is that the best solution? Or are there are best practices? The reason I ask is because it seems very clean in the view, but very bulky in the controller, and also it gets quite unwieldy when you have a much more complex set of nested tables, which is what I actually have (i.e., the data hash would be quite funky to put together)
First of I would use associations between item and the 2 other classes, so that you can do
item.refund
item.amount
Instead of Refund.where(...). You could further define methods such as
def refund_amount
refund.amount
end
And similarly for the other one (and hopefully come up with a better name than amount_amount.
This keeps both your view and controller clean but it won't be any faster. So far all of the approaches involve running 2 database queries per item which is the real issue as far as I'm concerned - whether those excess queries happen in the view or the controller is of lesser concern.
However you can avoid this with Active Record's include mechanism:
Item.include(:amount,:refund).where("your conditions here")
Will load the named associations in bulk rather than loaded them one at a time as each item is accessed.

I want to make a helper and template to dynamically generate a view for a collection of active record objects

This may sound strange, but none the less I want to learn how to do it and I need some help getting there. I'm not sure how to approach this. I'm hoping to get some dev love on this.... Let me explain by giving an example. (Btw thank you---you are awesome!)
Instead of this in my view:
<table>
#users.map do |user|
...
</table>
I want to extract it away into a helper that I can reuse for other collections.
So I want to say instead:
#users.to_table({
template: "simple_template",
header: ["Full Name","Email"],
column: ["name", "email"]
})
So in my application_helper I have something like this: (pseudo-ish code)
class ActiveRecord::Relation
def to_table *args
load args.template
self.map do |j|
args.header do |header|
j.header
end
args.column do |column|
j.column
end
end
end
end
I have no idea how to wire this up. (helper or table template) Definitely an order of magnitude above my current skill level. Need some serious direction.. I'm asking this because I feel like I hit a learning plateau and need help busting through to something more challenging (hence this question)... Hope it's clear, if not ask for clarification. Thanks for reading... Thanks for helping! =)
Not guaranteeing this will work it is just to show the syntax issues:
class ActiveRecord::Relation
def to_table(options={})
load options[:template]
self.map do |j|
Hash[
args[:headers].zip(args[:columns].map{ |column| j.send(column) }
]
end
end
end
Not Sure about the load part I think this should be handled outside of the relation as it is a view issue and has nothing to do with the ActiveRecord::Relation but this method will return an Array of Hashes like
[{"Full Name" => "USER 1 Name", "Email" => "USER1#email.com},{"Full Name" => "USER 2 Name", "Email" => "USER2#email.com"}]
In your current method args which is an array now based on the * will not respond to things like template or column. Like I said I have never really tried to implement anything in this way but the syntax change might get you headed in the right direction. Also handling should be put in place for when template is not passed or headers.count != columns.count.
Best bet is probably something like this
<%= render "template", obj: #user.to_table(headers: ["Full Name","Email"],columns: ["name", "email"]) %>
in _template.rb
<table>
<thead>
<tr>
<% obj.first.keys.each do |header|
<th><%= header %></th>
<% end %>
</tr>
</thead>
<tbody>
<% obj.each do |row|
<tr>
<% row.values.each do |cell| %>
<td><%= cell %></td>
<% end %>
</tr>
<% end %>
</tbody>
Although if I had more time to think there are probably far simpler implementations of this maybe something like
<%= render 'template', locals:{collection: #users, headers: ["Full Name","Email"], columns: ["name", "Email"]} %>
UPDATE
I think making a view helper might be better in this instance like this
def make_table(collection,options={})
content_tag(:table,options[:table_options]) do
content_tag(:thead) do
content_tag(:tr) do
options[:headers].map do |header,header_options|
content_tag(:th,header,header_options,false)
end.join.html_safe
end
end
content_tag(:tbody,options[:body_options]) do
collection.map do |obj|
content_tag(:tr,options[:row_options]) do
options[:columns].map do |column,cell_options|
content_tag(:td,obj.public_send(column),cell_options,false)
end.join.html_safe
end
end.join.html_safe
end
end
end
call as
<%= make_table(#users,columns:{name:{class: "name"},email:{}},headers:{"Full Name"=>{class:"name_header"},"Email"=>{}}) %>
or without formatting
<%= make_table(#users,columns:[:name,:email],headers:["Full Name","Email"]) %>
This method requires an object collection and will accept the following through the options Hash
:table_options as a Hash to pass to the content tag for formatting the table
:headers as an Array or Hash (for formatting header rows)
:body_options as a Hash to pass to the content tag for formatting the table body
:row_options as a Hash to pass to the content tag for formatting the rows
:columns as an Array or Hash (for formatting the individual cells)
You can place this method in helpers/application_helper.rb and you will have access to it throughout the application. Although I have not fully vetted this method and it is currently more conceptual than anything else.

Ruby generate table from date range

I've been having a lot of trouble with this:
For an RPI application, all my results are within one table: results
I'm trying to print a table based on a specific data range: 2011-07-31..2012-07-01
In my controller:
class SeasonsController < ApplicationController
def s2012
#results = Result.all
#s2012 = Result.where(:date => (2011-07-31)..(2012-07-01))
end
end
In my view:
<table>
<tr>
<th>Event ID</th>
<th>Date</th>
</tr>
<% #s2012.each do |result| %>
<tr>
<td><%= result.event_id %></td>
<td><%= result.date %></td>
</tr>
<% end %>
</table>
This doesn't output any errors (small miracle), but nothing is displayed in the view. #results = Result.all prints all the whole table just fine. But how can I limit it to a specific data range?
I know you're trying to do this all ActiveRecordy, but imo it's actually less readable than if you just wrote the SQL where clause yourself.
Result.where('date BETWEEN ? AND ?',
Date.parse('2011-07-31'),
Date.parse('2012-07-01'))
maybe try:
Date.parse('2011-07-31')..Date.parse('2012-07-01')
In your current example rails will think you are doing arithmetic. You are looking at a date between the number 1973 and 2004. Instead you want to convert them to date objects first. Do
#s2012 = Result.where(:date => DateTime.parse('2011-07-31')..DateTime.parse('2012-07-01'))

Controller and view association and self.method still confuses me

Okay so in the index portion of my controller I set
#patients = Patient.all
then in patients_helper.rb
def race_abrev
return self.caucasian
end
where caucasian is an integer datatype column in the patients table
then in the view index.html.erb
<% #patients.each do |p| %>
<td><%= p.gender %></td>
<td><%= p.ethnicity %></td>
<td><%= p.race_abrev %></td>
<% end %>
I get a
undefined method `race_abrev' for #<Patient:0xb4d95cd8>
I've checked the table and I'm expecting patient.caucasian to return the integer 1, what am I missing..any insight to a fundamental misunderstanding I seem to have?
race_abrev is a helper, not a method on Patient:
<%= race_abrev(p) %>
And the helper itself would return p.caucasian, although it'd seem like you'd actually want to do something with the value of caucasian, like a compare or something.
All this said, I'm not sure why you're not defining it (or what "it" actually is) on the model, since so far it doesn't seem to have anything to do with the view, which is what view helpers are for.
If you're storing something in the DB you want to transform it may or may not belong in a view helper; if it's to turn it into something human-readable I'd be more likely to put it in the model.

Resources