Iam having an issue getting data from two different database tables to display properly with in an HTML table. I'm sure I am just overlooking something and this is pretty easy, but for whatever reason I cannot get quantity to display, or the <td class="sub-total"> to display either.
Demands (you can think of these as orders) and items are connected through a table called demand_items. The demand_items table also has the quantity of the item ordered. The page will display but the quantity and the subtotal will not render.
Here is the html:
<h3>Order for <%= #demand.customer.name %></h3>
<h4>Ordered for: <%= #demand.date %></h4>
<h4>Add Items</h4>
<%= render 'demands/item_form' %>
<table class="customer-table">
<tr>
<th>Item</th>
<th>Price</th>
<th>Quantity</th>
<th>Sub Total</th>
</tr>
<% #demand.items.each do |item| %>
<tr>
<td><%= item.name %></td>
<td><%= item.price %></td>
<% #demand.demand_items do |quantity| %>
<td><%= quantity.quantity %></td>
<td class="sub-total"><%= (item.price) * (quantity.quantity) %></td>
<% end %>
<% end %>
</tr>
</table>
#demand.demand_items.each do |quantity|..........instead of #demand.demand_items do |quantity|......
Related
I'm creating a rails app for my expenses. I have several models like user, spending, currency. I put relations between them, everything works. I can add a new expense that is shown on a index (all the expenses from all users) and I decided to loop through the expenses on the user show page, where the current_user see his own expenses.
I also grouped the expenses by date (month and year) and I have a "Today" group, which shows all the expenses entered on the day. It looks like this in the controller :
def show
#user_spendings = #current_user.spendings.all.order('date DESC').paginate(:page => params[:page], :per_page =>
10)
#Retrives all messages and divides into two groups todays messages and other messages
#grouped_spendings = #user_spendings.group_by{ |t| t.date.to_date == DateTime.now.to_date }
if #user_spendings.present?
#Create month wise groups of messages
#month_wise_sorted_spendings = #user_spendings.group_by{ |t| (Date::MONTHNAMES[t.date.month] + " " + t.date.year.to_s) }
end
end
This is my view :
<% if #grouped_spendings.present? && #grouped_spendings[true].present? %>
<table>
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Amount</th>
<th>Currency</th>
<th>Created</th>
<th>Actions</th>
</tr>
</thead>
<h3>Today</h3>
<tbody>
<% #grouped_spendings[true].each do |spending| %>
<tr>
<td><%= spending.title %></td>
<td><%= spending.description %></td>
<td><%= '%.02f' % spending.amount %></td>
<td><%= spending.currency.symb %></td>
<td><%= spending.date.strftime('%d %B %Y') %></td>
</tr>
</tbody>
<% end %>
</table>
<% end %>
<% if #month_wise_sorted_spendings.present? %>
<% #month_wise_sorted_spendings.each do |hash_elements|%>
<table>
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Amount</th>
<th>Currency</th>
<th>Created</th>
<th>Actions</th>
</tr>
</thead>
<% date_in_link = "#{hash_elements.first}" %>
<h3><%= link_to(date_in_link, detail_result_path) %></h3>
<tbody>
<% hash_elements.last.each do |spending| %>
<tr>
<td><%= spending.title %></td>
<td><%= spending.description %></td>
<td><%= '%.02f' % spending.amount %></td>
<td><%= spending.currency.symb %></td>
<td><%= spending.date.strftime('%d %B %Y') %></td>
</tr>
</tbody>
<% end %>
</table>
<% end %>
<% end %>
I would like to create a link on the month and year and open a page where all the informations are looped again but only for this specific month of the year.
I already achieved the linking with this :
<% date_in_link = "#{hash_elements.first}" %>
<h3><%= link_to(date_in_link, detail_result_path) %></h3>
To avoid mixing the controllers etc... I decided to create new controller called detail and a result.html.erb page where I will loop the expenses about the specific month and year.
Which way should I take with this ? Is it a good option to do a new controller ? And how can I get the infos back according to the date on my new page ?
I thought about passing some params via the link_to but I'm not sure it's the best solution. Maybe there is something easier but I don't know or see it.
If anyone can help would be great !
Thanks alot
I found the answer but it's on a other post I did as I had an other problem before this one. So the link is here
Feel free to ask question if any
I realize the heading is a little confusing but my problem is quite simple. I hae two models in my rails 5 app. User and Expense. Each expense belongs_to a user. I have an index page where all expenses are being listed. I can list the user IDs for each expense from the expenses table but I want to instead look up the name of the user (in column username) in the users table and display it with the expense instead. The view I have written is below. But it doesn't work.
<p id="notice"><%= notice %></p>
<h1>Teamjournals</h1>
<table style="padding: 2px; width: 50%" border="2px" align="center">
<thead>
<tr>
<td align="center"><%= link_to new_expense_path, :class =>"btn btn-success btn-wide" do%>Add New Expense<% end %></td>
</tr>
<tr>
<th>User</th>
<th>Expense Date</th>
<th>Currency</th>
<th>Expense Amount</th>
<th>Description</th>
<th colspan="1"></th>
</tr>
</thead>
<tbody>
<% #expenses.each do |expense| %>
<tr>
<td><%= User.joins(:expense).where('expense.user_id = ?', #user.id) %></td>
<td><%= expense.expense_date %></td>
<td><%= expense.currency.currency %></td>
<td align="right"><%= expense.expense %></td>
<td><%= expense.description %></td>
</tr>
<% end %>
</tbody>
</table>
Ok so in your iteration over #expenses you have this line:
<%= User.joins(:expense).where('expense.user_id = ?', #user.id) %>
you can change it to this:
<% user = expense.user %>
Note that I'm using <% not <%= because I'm just trying to assign a variable, not print the output to html.
Then after defining user you can say <%= user.name %>.
You should read a bit more about active record associations, but here's a few side comments about the query you've shown
User.joins(:expense).where('expense.user_id = ?', #user.id)
In this case, you should use the method generated by belongs_to instead of writing a query. But in situations where you do want to write a custom query, you should only be using where when you want to get an array. In this case you're looking for a single record so you could use find_by. Furthermore, the joins you're doing here is unnecessary
# any of these works
user = User.where('id = ?', expense.user_id).first
user = User.where(id: expense.user_id).first
user = user.find_by(id: expense.user_id)
My view shows a table of products that are returned in a search, as well as their respective details, the vendors who sell those products, and their associated price. What I'm trying to do is to put vendors and price in a dropdown, rather than have two data cell that have 5+ vendors and prices distorting the table row height. Is this possible? What would be the best approach? I've looked at using a list, but not sure how to get both price and vendors in a dropdown together that way. I'm currently using a table within a table (table inception), but please let me know if you think there's a better way. Here's my current view:
<table>
<tr class="search-table">
<td>Product</td>
<td>Details</td>
<td>Brand</td>
<td>ID</td>
<td>Vendors</td>
</tr>
<% #search_res.each do |item| %>
<tr class="search-table">
<td><%= item.product %></td>
<td><%= item.details %></td>
<td><%= item.brand %></td>
<td><%= item.id %></td>
<td>
<table>
<tr>
<% item.vendors.each do |vendor| %>
<td><%= vendor.name %></td>
<% end %>
</tr>
<tr>
<% item.inventory_items.each do |product| %>
<td><%= product.price %></td>
<% end %>
</tr>
</table>
</td>
</tr>
<% end %>
</table>
Thanks in advance!
I think there's some errors in markup worthy to mentioned before all:
your css class "search-table" possibly is not assumed to be applied to a table's row:
<tr class="search-table">
Header row semantically better to wrap in "thead" tag and columns in the row markup as "th" tag: <thead> <tr class="search-table"> <th>Product</th> <th>Details</th> <th>Brand</th> <th>ID</th> <th>Vendors</th> </tr><thead>
on the matter of question, I think that using Bootstrap framework and dropdowns wouldn't be convienient. IMHO collapsible elements would make the trick of show of some extra info.
Just wrap collpsible divs into table's cell, it will look something like that:
http://jsfiddle.net/aizekAzimov/9LHw2/
hope it'll help
I have a loop in one of my views to display a table like so:
Each category object has 5 attributes called: level_1, level_2, level_3, level_4, level_5.
There will be an array with all the category objects. so there could be any amount of categories and no necessarily 3.
what would the best way to draw this up be? i have something like this at the moment but dont know how to select the relevant category level attribute in the 5.times loop.
<table border="0">
<tr>
<th>Maturity</th>
<% for category in #categories %>
<th><%= category.category_title %></th>
<% end %>
</tr>
<% 5.times do |i|%>
<% level = 5 - i %>
<tr>
<td>Level <%= level %> Maturity</td>
<% for category in #categories %>
<td><%= category.level_ #put in the level value here so it selects the relevant attraibute %></td>
<% end %>
</tr>
<% end %>
</table>
you need to change the rendering of level with this:
<% for category in #categories %>
<td><%= category.send("level_#{level}") %></td>
<% end %>
send is used to call a method on an object so that you can compose your method at runtime.
If you categories as variable no. then you shouldn't make it columns, but rows. And then levels will be you columns e.g.
<table>
<tr>
<th>Level 1</th>
<th>Level 2</th>
<th>Level 3</th>
<th>Level 4</th>
<th>Level 5</th>
<tr>
<% #category.each do |c| %>
<tr>
<td>#category.level_1<td>
<td>#category.level_2<td>
<td>#category.level_3<td>
<td>#category.level_4<td>
<td>#category.level_5<td>
<th>
<% end %>
Now in above code you may replace hard coded level_#{no} with iterations.
I currently have an Array that contains some URL's to images.
#images = [ "http://site/images/01.jpg", "http://site/images/02.jpg" ]
Total 18 images
I would like to take this array and create a thumbnail gallery where the gallery is 3 columns across in my view. The HTML out put would be
<table>
<tr>
<td><img src="http://site/images/01.jpg"></td>
<td><img src="http://site/images/02.jpg"></td>
<td><img src="http://site/images/03.jpg"></td>
</tr>
<tr>
<td><img src="http://site/images/04.jpg"></td>
<td><img src="http://site/images/05.jpg"></td>
<td><img src="http://site/images/06.jpg"></td>
</tr>
</table>
My current implementation gets me a one column table
<table>
<tr>
<% #images.each do | image | %>
<td><%= image_tag(image)%></td><br>
<% end %>
</tr>
</table>
In the future I might want it to be 6 columns instead of 3 columns.
I'm looking for a way to do this in a clean and flexible way.
I was looking at the Ruby documentation and I saw this
Class Range (rng.step method)
http://www.ruby-doc.org/core/classes/Range.html
Not sure if this Range class step method can solve the problem but the example it provides is interesting.
Any thoughts, I'm still learning and maybe I'm over thinking this?
use each_slice()
<table>
<% #images.each_slice(3) do |images| %>
<tr>
<% images.each do |image| %>
<td><%= image_tag(image) %></td>
<% end %>
</tr>
<% end %>
</table>
Untested. You need to use each_with_index and the modulus % operator.
<% #images.each_with_index | image, index | %>
<% unless index % column_count == 0 %>
<td><%= image_tag(image)%></td>
<% else %>
<td><%= image_tag(image) %></td>
</tr>
<tr>
<% end %>
<% end %>