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
Related
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.
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
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 the same problem which keeps coming up in my Ruby-on-Rails app (my first app, which I inherited). I have an admin dashboard where the view iterates over the users and puts some data about them up in an html table, one row per user. Currently, I have the view running the iteration in embedded ruby, and looking for the data on the user, sometimes processing it, and putting it in the table, row by row as it iterates.
I'm pretty new to rails, but it seems like having the program do so much of the database calls from the view seems like it is breaking the MVC model, and not very secure. I'm now trying to use some data that I will be calling from our payment processing company (Stripe), and it seems like it is very insecure to do so from the View. Specifically, I'm planning to add a column with customers locations, based on the zip code they gave stripe. This requires calling Stripe for the credit card associated with their account.
Here's the problem. I see three potential ways to do it, only one of which works (but I think it's the wrong one, so I'm hoping you can show me another way.)
Iterate over the users in the controller. Fetch the data needed, and do... something with it that lets me had it to the View. Iterate over the users in the view, fetching the data only associated with that user from the controller, and creating one table row per user. Again, not sure how to do that.
Iterate over the data in the controller, and somehow feed the data to the view where it knows it is receiving data and it should put each iteration in a row (and each item from the iteration in a cell. Is this possible?
What I'm doing now: Do nothing in the Controller except creating a local variable of all my users #users=User.all, do everything in the view.
Right now, the code in the view (for one of these examples. I have this happening several times in several Views, accessing all our users on a users as customers page, a users and community engagement page, and another page for every product.) looks like this:
<table id="table_of_users2">
<thead>
<th width="200px">First Name</th>
<th width="200px">Last Name</th>
<th width="200px">Email</th>
<th width="200px">Number Representing Community Activity</th>
<th width="200px">Favorites</th>
<th width="200px">Comments</th>
</thead>
<tbody>
<% #users.each do |user|%>
<% #our_community_activity_number=0 %>
<% #times_faved=0 %>
<% #comments=0 %>
<% user.engagements.each do |engagement| %>
<% if engagement.our_community_activity==true %>
<% #our_community_activity_number+=1%>
<% end %>
<% if engagement.favorite==true%>
<% #times_faved+=1%>
<% end %>
<% if engagement.comment!="" || engagement.comment!=nil %>
<% #comments +=1 %>
<% end %>
<% end %>
<tr>
<td width="200px"><%= user.first_name%> </td>
<td width="200px"><%= user.last_name %></td>
<td width="200px"><%= mail_to("#{user.email}")%></td>
<td width="200px"><%= #our_community_activity_number %></td>
<td width="200px"><%= #times_faved==0?0:#meals_faved %>
<td width="200px"><%= #comments %></td>
</tr>
<% end%>
</tbody>
</table>
How can I rewrite this so that it is more secure and I can safely add their location information from the payment processing company to the page? Am I correct in thinking I can't safely do that in this way?
Thanks. I'm new at Rails, and I keep running into this same problem again and again.
I dont' see anything horribly wrong with this. There's no data access happening in the views, but there is some logic. If you wanted to remove the logic, you
UserDashboardModel = Struct.new(:first_name, :last_name, :email, :our_community_activity_number, :times_faved, :comments)
def dashboard
#users = User.all.map do |user_record|
UserDashboardModel.new.tap do |user|
user.first_name = user_record.first_name
user.last_name = user_record.last_name
user.email = user_record.email
user.our_community_activity_number = user_record.engagements.select(&:our_community_activity).count
user.favorite = user_record.engagements.select(&:favorite).count
user.comments = user_record.engagements.select{|e| e.comment.present?}.count
end
end
end
then in the view, it's much simpler:
<table>
<thead>
<th width="200px">First Name</th>
<th width="200px">Last Name</th>
<th width="200px">Email</th>
<th width="200px">Number Representing Community Activity</th>
<th width="200px">Favorites</th>
<th width="200px">Comments</th>
</thead>
<tbody>
<% #users.each do |user| %>
<tr>
<td width="200px"><%= user.first_name%> </td>
<td width="200px"><%= user.last_name %></td>
<td width="200px"><%= mail_to(user.email)%></td>
<td width="200px"><%= user.our_community_activity_number %></td>
<td width="200px"><%= user.favorite %>
<td width="200px"><%= user.comments %></td>
</tr>
<% end%>
</tbody>
</table>
Is this better? subjectively --- the view is definitely dumber. That's a plus. the controller is harder to reason about though -- there are ways around this, like building your UserDashboardModel array in another method somewhere.
Ruby is notorious for it's thin controllers, it's not a bad thing it's just the way active_model applications tend to set up :)
You should be doing a lot of this heavy lifting in your model, not your view or controller. You should ask the user for it's engagement_metrics and this is where a user loops over it's engagements and figures out the our_community_activity_number, times_faved and comments.
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 :)