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| %>
Related
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>
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
I'm still new to Rails, and can't seem to wrap my head around this.
I have table showing of several Products with several attributes in form columns. In my index view I'm showing a shortened table, with just some of the columns. In my products#show I'm telling the full story, but obviously only for the selected Product. What I want to do, is letting users select a number of Products in products#index, and then store the selected IDs in a hash, sending them to another view, which I want the IDs to be presented in a full scale table, telling the whole story, of several products at once. Basically this is for comparing Products.
I'm not sure how to tackle this. As I'm already using DataTables, using javascript would make sense, but I'm unsure on how. And ideas? I've tried several ideas, but none of them got me anywhere.
products#index
<table id="products"cellpadding="0" cellspacing="0" border="0" class="table.responsive">
<thead>
<tr>
<th></th>
<th>Product Name</th>
<th>TG</th>
<th>TD</th>
<th>DK</th>
<th>DF</th>
<th>T260</th>
<th>T288</th>
<th>CTI</th>
<th>Strength</th>
<th>Rigidity</th>
<th>Standards</th>
<th>Manufactorer</th>
</tr>
</thead>
<tbody>
<% #products.each do |product| %>
<tr>
<td><%= check_box_tag "product_ids[]", product.id %></td>
<td><%= link_to product.name, product %></td>
<td><%= product.TG %></td>
<td><%= product.TD %></td>
<td><%= product.DK %></td>
<td><%= product.DF %></td>
<td><%= product.T260 %></td>
<td><%= product.T288 %></td>
<td><%= product.CTI %></td>
<td><%= product.ElectricStrength %></td>
<td><%= product.rigidity %></td>
<td><% product.standards.each do |standard| %>
<%= link_to standard.name, standard %>,
<% end %> </td>
<td><%= product.producer %></td>
<% end %>
</tr>
</tbody>
</table>
If anyone could spare some time to point me in the right direction, that would be wonderful!
This shouldn't be too tough to tackle, I'll walk you through the basic steps that I would take.
My basic approach would be to take a form of said checkboxes and submit it to an action designed to handle them in the way described, like so:
Create a new route to use for all of this. Take a look at 2.10.2 Adding Collection Routes in the docs (assuming you are using resources). Maybe call it something like "compare".
Use a form not backed by a model to hold your checkboxes and labels (1 of each for each product) - it should post to your new route.
In your controller, add the action for your route. This action should fetch the ids passed in by the form from the params and load the relevant products (remember that find can take an array of ids, so it should be pretty easy).
Finally, add a view for your new action, with the relevant logic to present your comparison.
You can easily come in an backfill this with javascript, but it is not required. Honestly, it would be more useful for adding a little extra panache, like disabling the button and showing a spinner, or something, than a full-on javascript submission, which doesn't really buy you anything in this particular case.
I am learning rails by trying to model a collectible card game.
I have a champion model and a rarity model. I have the has_many/belongs_to in the model definition and this works in the console:
c = Champion.find(1)
c.rarity.name
=> "Uncommon"
When I do the same thing in a template, I get
<%= champion.rarity.name %>
undefined method `name' for nil:NilClass
Any ideas on how to get this to work?
This is on Rails 3.2.2.
Update: Full .erb code
<% #champions.each do |champion| %>
<tr>
<td><%= champion.name %></td>
<td><%= champion.rarity.name %></td>
</tr>
<% end %>
If every Champion does not have a Rarity association (some are nil), you can use a .try() to print the name. Otherwise the .each will fail with a NoMethod when one without a Rarity is encountered.
<% #champions.each do |champion| %>
<tr>
<td><%= h champion.name %></td>
<td><%= h champion.rarity.try(:name) %></td>
</tr>
<% end %>
Or the less clever unless nil method:
<% #champions.each do |champion| %>
<tr>
<td><%= h champion.name %></td>
<td><%= h champion.rarity.name unless champion.rarity.nil? %></td>
</tr>
<% end %>
Note: I have also added the h() helper method to encode these for HTML output, though this is done automatically in Rails 3.
Right now, if I go to the index action of a model that I have, I don't show the basic table of data that rails generates for me if there are no existing records in the database. I do something like:
<% if #my_records.count > 0 %>
<table>
<tr>
<th>...</th>
<th>...</th>
<th>...</th>
<th>etc</th>
</tr>
<% #my_records.each do |my_record| %>
<tr>
<td><%=h my_record.etc %></td>
<td><%=h my_record.etc %></td>
<td><%=h my_record.etc %></td>
<td><%=h my_record.etc %></td>
</tr>
<% end %>
</table>
<% end %>
This works locally. However, when I push my app to heroku, this causes a 500 error and the log says:
ActionView::TemplateError (undefined method 'count' for []:Array) on line ...
So I change it to .length and it works fine. Can anyone tell me why that is? Someone has told me that these were redundant and rails got rid of .count but my understanding was that .length is an Array function that tells you how many items are in the Array and .count was an ActiveRecord method for determining how many items in the array were actual records in the database.
Can anyone shed light on this for me?
That's ruby issue, not rails. Locally you probably have 1.8.7, and heroku has 1.8.6. The Enumerable#count method was introduced in 1.8.7: compare http://ruby-doc.org/core-1.8.6/classes/Enumerable.html and http://ruby-doc.org/core-1.8.7/classes/Enumerable.html.