So, I have a list of words in a Words table.
I want to create 20 (x by x) tables for bingo cards with the words placed in the cells randomly on each card, with the center cell having a fixed "free space" value.
I think the words need to be placed in an array, but how do I ensure the location of the free space?
<%
cell_length = n
row_length = m
if m * n % 2 == 0
middle_cell = (m * n)/2
else
middle_cell = (m * n + 1) /2
end
count = 0
%>
<table>
<% for i in 1..m %>
<tr>
<% for j in 1..n %>
<% count +=1 %>
<td>
<%= count == middle_cell ? nil : 'word' %>
<td>
<% end %>
</tr>
<% end %>
</table>
Related
I'd like to create a todo list looking for any cell that matches today's date on either agreement, start or due date.
<% #project.where("project_date_agreement + project_date_start + project_date_due > ?", Date.today).each do |tasks_today| %>
<ul>
<li>Item Due Today</li>
</ul>
<% end %>
Any help getting me in right direction would be appreciated.
You want to use OR conditions in your where clause. Properly speaking this would be in your controller.
#projects = Project.where('date_agreement = ? OR date_start = ? or date_due = ?', Date.today, Date.today, Date.today)
In your Project Model you might want to create a field that say's what's due...
def due_because
due_array = []
due_array << 'Agreement date' if date_agreemnt == Date.today
due_array << 'Start date' if date_start == Date.today
due_array << 'Due date' if date_due == Date.today'
due_array.join(', ')
end
Then in your view you would iterate over the #projects
<ul>
<li>Items Due Today</li>
<% #projects.each do |project| %>
<li><%=project.name%> <%=project.due_because%></li>
<% end %>
</ul>
if you asking one row and different column, how about if you combine with table
<% #tasks = #projects.where("date_agreement = ? AND date_start = ? AND date_due = ?",Date.today ,Date.today, Date.today) %>
<table>
<tr>
<ul>
<% #tasks.each do |task| %>
<td>
<li><%= task.check_box :item_due %></li>
</td>
<% end %>
</ul>
</tr>
</table>
in view code:
<% for item in #cart.line_items %>
<%= item.quantity*item.product.price %>
<% end %>
I want to get the total price after calculation and show in a label.
#cart.line_items.sum { |item| item.quantity * item.product.price }
I don't know how appropriate title for this question.
Well, This is my problem
I have index action on homes_controller.rb looks like :
def index
#r = 2
#datas = [[1, 10], [2, 10], [3, 15], [4, 20], [5, 70], [6, 180], [7, 250], [8, 270], [9, 230], [10, 40], [11, 0], [12, 10]]
end
I want to result from above like this:
And on view looks like :
<table>
<thead>
<tr>
<th>Month</th>
<th>Forecast</th>
<th>Order</th>
<th>Begining Inventory</th>
<th>Ending Inventory</th>
</tr>
</thead>
<tbody>
<% ei = 0 %>
<% #datas.each_with_index do |d, index| %>
<tr>
<td><%= d[0] %></td>
<td><%= d[1] %></td>
<td><% if d[1] > bi %>
<% #datas.transpose.at(1).slice(index, #r).inject do |sum, s| -%>
<%= #biu = sum + s %>
<% end %>
<% end %>
</td>
<td><%= #bi = ei + #biu %></td>
<td><%= #ei = #bi - d[1] %></td>
</tr>
<% #biu = 0 %>
<% ei = #ei %>
<% end %>
</tbody>
</table>
State :
When the forecast d[1] is bigger than ei the calculation order #biu is 2 (#r) next of forecast.
Begining Inventory #bi is calculation from ei plus order #biu
Ending Inventory #ei is calculation from #bisubtracted with forecast d[1]
And then #ei put to ei
But the result like this :
I want 12th months must have : Order = 10, Begining invetory = 10, and Ending Inventory = 0
Your problem is here:
<% #datas.transpose.at(1).slice(index, #r).inject do |sum, s| -%>
<%= #biu = sum + s %>
<% end %>
When an array has only one item, inject without a starting value does not run the block at all:
puts [1,2].inject { |sum, s| puts 'was here!'; sum + s }
# was here!
# 3
puts [1].inject { |sum, s| puts 'was here!'; sum + s }
# 1
puts [1].inject(0) { |sum, s| puts 'was here!'; sum + s }
# was here!
# 1
If you add a starting value 0 to your code, though, #biu will be printed twice. To avoid that you need to move the assignment outside of the block:
<% #biu = #datas.transpose.at(1).slice(index, #r).inject do |sum, s| -%>
<% sum + s %>
<% end %>
<%= #biu %>
Or, more succinctly:
<%= #biu = #datas.transpose.at(1).slice(index, #r).inject(:+) %>
I am trying to make a table with ten values across on each row starting at 1 and going to 100.
My Ruby code looks like this:
<table border="1">
<% (1..100).each do |i|
d3 = (i % 3 == 0)
d5 = (i % 5 == 0)
i = "<b>#{i}</b>" if d5
i = "<i>#{i}</i>" if d3 %>
<tr>
<td><%= i %></td>
</tr>
<% end %>
</table>
How would I put this in an HTML table in a 10 X 10?
Using ERB:
<table>
<%10.times do |row|%>
<tr>
<%10.times do |col|%>
<td><%=
i = row*10+col+1
if i%5==0
"<b>#{i}</b>"
elsif i%3==0
"<i>#{i}</i>"
else
i
end
%></td>
<%end%>
</tr>
<%end%>
</table>
Using Haml:
%table
- 10.times do |row|
%tr
- 10.times do |col|
%td
- i = row*10+col+1
= i%5==0 ? "<b>#{i}</b>" : i%3==0 ? "<i>#{i}</i>" : i
<table border="1">
<% (1..100).each do |i|
d3 = (i % 3 == 0)
d5 = (i % 5 == 0)
s = "#{i}"
s = "<b>#{i}</b>" if d5
s = "<i>#{i}</i>" if d3 %>
<% if i % 10 == 1 %><tr><% end %>
<td><%= s %></td>
<% if i % 10 == 0 %></tr><% end %>
<% end %>
</table>
Basically, you want to start a table row before elements 1, 11, 21, etc. and end a row after elements 10, 20, 30, etc.
Is there a way with ActiveRecord to execute a custom SQL query and have it return an array of arrays where the first row is the column names and each following row is the row data? I want to execute something like:
connection.select_rows_with_headers "SELECT id, concat(first_name, ' ', last_name) as name, email FROM users"
And have it return:
[["id","name","email"],["1","Bob Johnson","bob#example.com"],["2","Joe Smith","joe#example.com"]]
This would allow me to print the results of the custom query in an HTML table like this:
<table>
<% result.each_with_index do |r,i| %>
<tr>
<% r.each do |c| %>
<% if i == 0 %>
<th><%=h c %></th>
<% else %>
<td><%=h c %></td>
<% end %>
<% end %>
</tr>
<% end %>
</table>
Note that select_all doesn't work because the keys in each hash are unordered, so you've lost the ordering of the results as specified in the query.
Not EXACTLY what you're looking for, but maybe:
connection.execute('select * from users').all_hashes
and you'll get back
[{:id => 1, :name => 'Bob', :email => 'bob#example.com'},{:id => 1, :name => 'Joe', :email => 'joe#example.com'}]
and you could do:
results = connection.execute('select * from users').all_hashes
munged_results = []
columns = results.first.keys.map(&:to_s)
munged_results << results.first.keys.map(&:to_s)
munged_results += results.map{|r| columns.map{|c| r[c]} }
something like that
edit:
results = connection.execute('select * from users').all_hashes
munged_results = []
columns = User.column_names
munged_results << columns
munged_results += results.map{|r| columns.map{|c| r[c]} }
That should be ordered properly.
Beyond that there is the result object that is returned from #execute that can be interrogated for bits of information. Methods like #fetch_fields get you the fields in order and #fetch_row will get you each row of the result set as an array (works like an iterator).
edit again:
OK, here's a good solution, modify for whatever DB you're using:
class Mysql::Result
def all_arrays
results = []
results << fetch_fields.map{|f| f.name}
while r = fetch_row
results << r
end
results
end
end
That will get them without a ton of overhead.
Use it like this:
connection.execute('select salt, id from users').all_arrays