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
Related
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)
So this may be slightly confusing but i hope to make it work fine.
So at the moment i have this a table, Which looks like this,
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>EventName</th>
<th>Description</th>
<th>Initial Provider</th>
<th>Link</th>
</tr>
</thead>
<tbody>
<% #newevents.each do |ne| %>
<tr>
<td><%= ne.id %></td>
<td><%= ne.product_name %></td>
<td><%= ne.description %></td>
<td><%= ne.merchant_name %></td>
<td><%= link_to "Edit Event", edit_event_path(ne.id), class: "btn btn-info" %></td>
</tr>
<% end %>
</tbody>
</table>
At the moment i have this loading in from DBTable1
Now, I'm wanting it so that when you click on the edit button. It will take you to a page where it can load in information from DBTable1 but save the information into DBTable2
At the moment. DBTable2 has a DBTable1_id field inside. But i haven't worked out fully how to use it. DBTable1 does have has_one DBTable2 inside
How do i go about making this so that when you edit your actually creating a new row in DBTable2 from the view?
Seems like you need to simply adjust the controller action (or create a new action) that acts just like "update", but creates a new one instead.
Actually, you can probably do this by changing edit_event_path(ne.id) to something else like, create_event_path(ne.id) or new_event_path(ne.id).
Although, don't know enough about your app (including what it's routes are) to be sure.
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
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|......
I currently have models that can be described as follows:
Songs can have many setlists through Allocations
Allocations belong to songs and setlists
Setlists can have many songs in them through allocations
Songs have a title, artist, and a musical key.
Basically I'm setting up the new setlist view where a musician can select any existing songs from the library to add to a setlist. I want to do something along these lines:
<thead>
<th>Title</th>
<th>Artist</th>
<th>Root Key</th>
</thead>
<tbody>
INSERT CODE HERE TO DISPLAY DATA
</tbody>
At the moment I'm using the following code to get the data but I don't know if there's a way to separate it out into the relevant cells in the table:
<% songs = Song.all.collect {|s| [ s.title, s.artist, s.key ] } %>
<% songs.sort! %>
I'm not sure if this is this is the best way to go about doing this so if anyone could suggest an alternative that would be fantastic too. Thanks in advance!
Fetching data is controller's responsibility.
def index
#songs = Song.select([:title, :artist, :key]).all
end
And view:
<tbody>
<% #songs.each do |song| %>
<tr>
<td><%= song.title %></td>
<td><%= song.artist %></td>
<td><%= song.key %></td>
</tr>
<% end %>
</tbody>
I managed to solve the problem as follows:
<tbody>
<% songs.each do |n| %>
<tr>
<td><%= n[0], '#' %></td>
<td><%= n[1], '#' %></td>
<td><%= n[2], '#' %></td>
</tr>
<% end %>
</tbody>
I'm still not sure whether or not this is best practice or not so if anyone know of a better way please let me know. Thanks :)