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 :)
Related
I wanted to show the others related Items of a same thing when finishing adding something.
I have a normal crud scaffold, so when I add a "Task" which is related to a project, I wanted to when it redirect to "Show" to a table be formed showing other "tasks" of the same related project...
show.html.erb
<table>
<thead>
<tr>
<th>Seq</th>
<th>Descr</th>
<th>Seqpai</th>
<th>Type</th>
<th>Hour</th>
<th>Pid</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #lookup.each do |lookup| %>
<tr>
<td><%= task.seq %></td>
<td><%= task.descr %></td>
<td><%= task.seqpai %></td>
<td><%= task.typo %></td>
<td><%= task.hour %></td>
<td><%= task.projeto.name %></td>
</tr>
<% end %>
</tbody>
tasks controller =>
def lookup
#taskete = Task.where(#projeto_id)
end
Tasks Belong to Projeto and Projeto has many Tasks
I don't understand what's the problem. But if the source code of your controller you define variable called "#taskete", then in the view you are iterating over "#lookup".
Your loop in view should be: #taskete.each do |task|
Tasks belong to projecto and projecto has many tasks. So to show all the tasks of the projecto of the current task do...
<% #taskete.projecto.tasks.each do |task| %>
If you only want to show the other tasks (i.e. don't show the current task in the list, only the related tasks) you may want to do...
<% #taskete.projecto.tasks.each do |task| %>
<% next if #taskete == task %>
...which will skip the current loop if the task in the collection is #tasketo
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
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'm fairly new to rails, and am still getting used to putting together methods. I'm currently trying to create a method that averages distinct data from multiple columns. I'd like to do it all in one method so that I can display the information easily in an html table.
Currently I have this in my model:
def averagedonate
scores.group(:donatedate).average('donateamount')
scores.group(:donatedate).average('rating')
end
I'd like to be able to use them in a table like this:
<% #averagedonate.each do |donatedate, donateamount, rating| %>
<tr>
<td><%= donatedate %></td>
<td><%= donateamount %></td>
<td><%= rating %></td>
</tr>
How do I change my averagedonate method to do this? Thanks in advance!
I haven't tested, but something to this effect should work
def averagedonate
scores.select("
AVG(donateamount) as avg_donateamount,
AVG(rating) as avg_rating,
donatedate
")
.group(:donatedate)
end
Then use it like this
<% #averagedonate.each do |item| %>
<tr>
<td><%= item.donatedate %></td>
<td><%= item.avg_donateamount %></td>
<td><%= item.avg_rating %></td>
</tr>
<% end %>
I am building an html table that should include name, rating1, rating2, and rating3. rating 1-3 come from different models than name.
resources :names do
resource :rat1,:rat2,:rat3
end
Inside of my html table I'd like to include the ratings from within each of these tables but I would like to automatically skip over or ignore tables that are nil. This is because :names may only have a :rat1 and not a :rat2 or :rat3. My view should look something like this.
<table>
<thead>Name</thead>
<thead>Rating 1</thead>
<thead>Rating 2</thead>
<thead>Rating 3</thead>
<% #names.each do |name| %>
<tr>
<td><%= name.nametext %></td>
<td><%= name.rat1.rating %></td>
<td><%= name.rat2.rating %></td>
<td><%= name.rat3.rating %></td>
</tr>
<% end %>
</table>
Except that if name.rat1 is nil it will either a.) replace the value with N/A OR b.) it will leave this field blank and move on to the next.
What is the cleanest way to do this?
::UPDATE::
So my issue is that the name.rat1 is nil and the name.rat1.rating is an undefined method of a nil class so both of these options will throw the same undefined method of a nil class error regardless of the || or helper method. At least thats what my current tests are showing. Any other options? or different workarounds? I'd like to avoid having to put a validation loop like this for every rat1-3
<% unless name.rat1.nil? %>
<%= name.rat1.rating %>
<% end %>
There has to be a simpler way.
I would probably create a helper method in names_helper.rb
def show_rating(rating)
if rating.present?
rating
else
"default value"
end
end
Then use it in the view:
<%= show_rating name.rat1.rating %>
OFFTOPIC Your table structure is wrong. It should have <thead><tr><th>Name</th><th>Rating1</th>..so on..</tr></thead>
So, in your case you can use the condition while rendering the rating values as:
<table>
<thead>
<tr>
<th>Name</th>
<th>Rating 1</th>
<th>Rating 2</th>
<th>Rating 3</th>
</tr>
</thead>
<tbody>
<% #names.each do |name| %>
<tr>
<td><%= name.nametext %></td>
<td><%= name.rat1.rating || 'N/A' %></td>
<td><%= name.rat2.rating || 'N/A' %></td>
<td><%= name.rat3.rating || 'N/A' %></td>
</tr>
<% end %>
</tbody>
</table>