Adding Time and Date to Paper_Trail data - ruby-on-rails

New to Rails, but learning a ton.
I'm using Paper Trail to save versions of my database, focusing on one sum that I want to maintain with each update. The sum is called "xoi_qb".
Within my model, I have defined the data I want to save like this:
def get_xoi_qb
xoi_qb = []
self.versions.each do |version|
unless version.reify.nil?
xoi_qb << version.reify.xoi_qb
end
end
return xoi_qb
end
Within my the html page that I want to display this data, I have this:
<p>Previous XOI</p>
<table summary="XOI Versions">
<tr>
<td><%= #quarterback.versions.map { |version| version.reify.xoi_qb} %></td>
</tr>
This is all working properly.
Alongside the "Previous XOI" data, I want to display the time and date that the "xoi_qb" sum is updated (I'm assuming by using "updated_at", but nothing I've tried is working).
Really appreciate your help.
(Side question: my "XOI" sum is appearing with the brackets (i.e. [745]) on the website...if you can tell me how to get rid of that, it would be awesome.)

I believe the date and time of the version is stored in the created_at field of the version. Did you try that?
As of your side question: you're basically doing [745].to_s in your view code, i.e. casting an array to a string, and it does what you're asking it to do. Your code looks like your intention was to output all the previous versions. For this to work you would rather iterate the array than convert it to a string. Something like:
<table>
<% quarterback.versions.each do |version| %>
<tr>
<td><%= version.reify.xoi_qb %></td>
</tr>
<% end %>
</table>
... And with dates:
<table>
<% quarterback.versions.each do |version| %>
<tr>
<td><%= version.reify.xoi_qb %> dated <%= version.created_at %></td>
</tr>
<% end %>
</table>

Related

Remove duplicates in view Rails

A minor, perhaps simple, question here. Let's say my DB returns duplicates. For example, I have multiple rooms that contain different start and end times.
My current view looks like:
<table>
<thead>
<tr>
<th>Location</th>
<th>Status</th>
<th colspan="1"></th>
</tr>
</thead>
<tbody>
<% #courses.each do |course| %>
<% if course.lec_exam.eql?("LEC")%>
<tr>
<td><%= course.location %></td>
<td><%= course.status %></td>
<td><%= link_to 'Edit Status', edit_course_path(course) %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
I'd like to clean this up a bit and remove the duplicates. Since each course has a location and start and end times, the same location will get displayed multiple times. What is the best approach to prevent this and display the unique locations, and then ensure that the status is correctly marked (i.e. closed means the current time is between the start and end time for each course that uses that location)? I have a few ideas but I'm not certain where to start. I can provide more information as needed.
Thanks!
Probably you can use Distinct SQL operator
http://guides.rubyonrails.org/active_record_querying.html
But I didn't get idea of your seconds part of question

Calling a helper method in view dynamically during loop with ruby on rails

<table>
<tr>
<th>Player Name</th>
<th>Club</th>
<th>Position</th>
<th>Points</th>
<th>Price</th>
</tr>
<% #myteam.each do |p| %>
<% #pd = playerDetails(p) %>
<tr>
<td><%= #pd.club %></td>
<td><%= #pd.name %></td>
<td><button>Remove</button></td>
</tr>
<% end %>
</table>
In the above I'm trying to get the players from myteam and put each in p. This works, I'm then trying to call a method on p called playerDetails and assign the values to #pd. This doesn't seem to work. Anyone know where exactly I've gone wrong? I only started learning ruby on rails today.
This is the playerDetails method by the way:
def playerDetails(pid)
#pd = Player.where(:id => pid)
end
helper_method :playerDetails
#pd will be an array. Try this
#pd = Player.where(:id => pid).first
A suggestion - You may want to fill the #myteam array with Player instances instead of player ids. It's faster to query for all the objects at once.

How do I extract relevant attribute values from a Rails habtm collection object in my view

I have 2 models:
1) upload
2) date_range
there is an intermediate join table as these models are associated by a many to many relationship thus, each is habtm to the other.
In my view for uploads(index.html.erb) Im trying to show all the date_ranges for a particular upload as follows:
<tr>
<th>File name</th>
<th>Upload Date, Time, Filename</th>
<th>Type</th>
<th>Dates in Upload</th>
<th>Total Rows</th>
<th>Rows Entered in DB</th>
<th>Percentage Completed</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<% #uploads.each do |u| %>
<tr>
<td> <%= u.sourcedata_file_name%></td>
<% path_arr = u.f_path.split("\/")%>
<td><%= path_arr[-3..-1]%></td>
<td> <%= u.sourcedata_content_type%></td>
=>> <td> <%= u.date_ranges.inspect%>
<td> <%= u.total_rows%></td>
<td> <%= u.rows_completed%></td>
like so.
This shows up as follows on the browser:
In my "Dates in Upload" column I want to only show some string with dates like this:
"2013-12-25, 2013-12-26" how do I only get these extracted out of the ActiveRecord::Associations::CollectionProxy object as it shows in the image?
Thanks
Use u.date_ranges.pluck(:date_range) to get just the date ranges.
you can then pretty it up with
u.date_ranges.pluck(:date_range).each {|range| puts range}
if you want them in a column.
I see you want them side by side, so it looks like there will only be two because it's a "range" so:
<%= u.date_ranges.pluck(:date_range).first %>, <%= u.date_ranges.pluck(:date_range).last %>
The simplest thing would probably be to add a to_s method in your DateRange model:
def to_s
date_range.to_s
end
And in your view, something like:
<%= u.date_ranges.map {|dr| dr.to_s }.join(', ') %>
However, that's really a bit too much code to put right in the view. Better would be to move that to a helper, or even use a presenter pattern. The 'draper' gem can make this kind of thing very easy, so you can do the same transformation in multiple places in your app, and keep your view template much cleaner.

Handling Yahoo Finance Quotes Hash in Rails 3

I am trying to manipulate the hash returned from Yahoo Finance for their Standard Quote using Ruby on Rails. I am new to Ruby and am getting a compile error in a view .erb file when I try to run the program. My objective is relatively straightforward - I want to display the Stock symbol, Bid and Ask prices and Corp name for each quote contained in the hash.
I stored the hash in an instance variable called #quote_info and pass this hash to the View.
The code in the view is as folows :
<h1>Stock Quote from Yahoo Finance</h1>
<p>Stock Symbol(s) Requested: <%= #quote_list %> </p>
<table>
<tr>
<th>Symbol</th>
<th>Bid Price</th>
<th>Ask Price</th>
</tr>
<% #quote_info.each |stock| do %>
<tr>
<td><%= stock.symbol %></td>
<td><%= stock.bid %></td>
<td><%= stock.ask %></td>
</tr>
<% end %>
</table>
I get a compile error on the each statement line , pointing to after the do term.
compile error
/home/lvl9/waf_projects/squotes_app/app/views/screenquotes/show.html.erb:18: syntax error, unexpected kDO
');#output_buffer.append_if_string= #quote_info.each |stock| do
^
Any thoughts would be much appreciated. I am tearing my hair out and cannot afford to lose any more.
Just a misplaced do:
<% #quote_info.each do |stock| %>

Returning a hash from controller to a view using a helper

Warning I'm brand new to rails!
While reading through a tutorial it has asked me to place a hash of string keys with decimal values into the products action method (My assumption they are talking about the "def products" in the controller.
In reguards to using the products method in the controller did I place my hash correctly?
In reguards to the placing the information from the hash into a table do I even need the helper method or is there a better way?
My helper needs help and doesn't format the data correctly using .html_safe I
This is what I have so far in my controler:
def products
#hard coded as products in controller
#stuff = {"a"=>200.00, "b"=>150.00, "c"=>100.00, "d"=>9.00, "e"=>15.00, "f"=>20.00 }
end
This is what I have in my product.html.erb file
<%= form_tag(products_path) do %>
<table id="aboutus_table">
<%= products_tabler() %>
</table>
<% end %>
and then the helper...it needs help
def products_tabler
snowholder = #snow_stuff.each {|key,value|puts "<tr><td>#{key}</td><td>#{value}</td><tr>"}
return snowholder
end
puts is probably a mistake, you don't really want to print to standard out in a web service. See if this works?
def products_tabler
snowholder = ""
#snow_stuff.each {|key,value| snowholder += "<tr><td>#{key}</td><td>#{value}</td><tr>"}
return snowholder
end
I realize this is a tutorial, but using a helper that emits hardcoded html is not an improvement over having the html in the view itself.
In this case, it's really simple to do it in the view:
<table id="aboutus_table">
<% #snow_stuff.each do |key, value| %>
<tr>
<td><%= key %></td><td><%= value %></td>
</tr>
<% end %>
</table>
If you really wanted to separate the creation of the rows, a collection partial would be better. Then Rails does the iteration for you. Use this technique when you've got real data (i.e. ActiveRecords instead of hashes).
<table id="aboutus_table">
<%= render :partial => "row", :collection => #stuff %>
</table>
Then the _row partial would contain:
<tr>
<td><%= row.name %></td><td><%= row.value %></td>
</tr>

Resources