Rails create table row on button click - ruby-on-rails

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.

Related

Display Table in "Show" Page using the same related ID

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

Remove duplicates in view Rails

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

How do I save dynamically generated API content from a view to a SQLite3 database?

I have an application where the user can search for albums by artist. My search makes an API call to iTunes and I display the top 25 results as a simple table:
<table>
<thead>
<tr>
<th>Artist</th>
<th>Album</th>
<th>Genre</th>
<th></th>
</tr>
</thead>
<tbody>
<% #search_results.each do |album| %>
<tr>
<td><%= album['artistName'] %></td>
<td><%= album['collectionName'] %></td>
<td><%= album['primaryGenreName'] %></td>
<td><%= link_to "Add to Library", albums_url, method: 'post' %></td>
</tr>
<% end %>
</tbody>
</table>
In the last column I want to have a button that the user could click which would save that row (a specific album) to the database. I would only be saving the three columns listed to the database.
I'm having a little trouble figuring out the logic behind scraping my own page and saving those objects, and how that would work in my controller. For reference I have a simple Rails 4 application using SQLite3 in development.
The dead simplest way to do this, no JavaScript etc., would be to make a link
= link_to 'Save', albums_url({ :album => { :artist_name => album['artistName'], :collection_name => album['collectionName'], :primary_genre_name => album['primaryGenreName'] }) , method: 'post'
where the hash argument to albums_url(hash_argument) is the parameters for the album.

Storing selected IDs, fetching to display in a custom routed view

I'm still new to Rails, and can't seem to wrap my head around this.
I have table showing of several Products with several attributes in form columns. In my index view I'm showing a shortened table, with just some of the columns. In my products#show I'm telling the full story, but obviously only for the selected Product. What I want to do, is letting users select a number of Products in products#index, and then store the selected IDs in a hash, sending them to another view, which I want the IDs to be presented in a full scale table, telling the whole story, of several products at once. Basically this is for comparing Products.
I'm not sure how to tackle this. As I'm already using DataTables, using javascript would make sense, but I'm unsure on how. And ideas? I've tried several ideas, but none of them got me anywhere.
products#index
<table id="products"cellpadding="0" cellspacing="0" border="0" class="table.responsive">
<thead>
<tr>
<th></th>
<th>Product Name</th>
<th>TG</th>
<th>TD</th>
<th>DK</th>
<th>DF</th>
<th>T260</th>
<th>T288</th>
<th>CTI</th>
<th>Strength</th>
<th>Rigidity</th>
<th>Standards</th>
<th>Manufactorer</th>
</tr>
</thead>
<tbody>
<% #products.each do |product| %>
<tr>
<td><%= check_box_tag "product_ids[]", product.id %></td>
<td><%= link_to product.name, product %></td>
<td><%= product.TG %></td>
<td><%= product.TD %></td>
<td><%= product.DK %></td>
<td><%= product.DF %></td>
<td><%= product.T260 %></td>
<td><%= product.T288 %></td>
<td><%= product.CTI %></td>
<td><%= product.ElectricStrength %></td>
<td><%= product.rigidity %></td>
<td><% product.standards.each do |standard| %>
<%= link_to standard.name, standard %>,
<% end %> </td>
<td><%= product.producer %></td>
<% end %>
</tr>
</tbody>
</table>
If anyone could spare some time to point me in the right direction, that would be wonderful!
This shouldn't be too tough to tackle, I'll walk you through the basic steps that I would take.
My basic approach would be to take a form of said checkboxes and submit it to an action designed to handle them in the way described, like so:
Create a new route to use for all of this. Take a look at 2.10.2 Adding Collection Routes in the docs (assuming you are using resources). Maybe call it something like "compare".
Use a form not backed by a model to hold your checkboxes and labels (1 of each for each product) - it should post to your new route.
In your controller, add the action for your route. This action should fetch the ids passed in by the form from the params and load the relevant products (remember that find can take an array of ids, so it should be pretty easy).
Finally, add a view for your new action, with the relevant logic to present your comparison.
You can easily come in an backfill this with javascript, but it is not required. Honestly, it would be more useful for adding a little extra panache, like disabling the button and showing a spinner, or something, than a full-on javascript submission, which doesn't really buy you anything in this particular case.

Twitter Bootstrap Dropdown in Table Data Cell

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

Resources