Making a table row into a link in Rails - ruby-on-rails

I am trying to make a row in a table link to the edit page. I know the links are being created, because I can print them out. I am close, but am missing something important. What do I change to make the link work properly?
<h1>Scouts</h1>
<p><%= button_to "Add a new Scout", new_scout_path, :method => :get %></p>
<div class="message-board">
<table>
<tr>
<th>Name</th>
<th>Rank</th>
<th>Advancement Date</th>
<th>Age</th>
</tr>
<% #scouts.each do |scout| %>
<tr <% link_to edit_scout_path(scout) %> >
<td><%= scout.name %></td>
<td><%= scout.rank %></td>
<td><%= scout.advancement %></td>
<td><%= scout.age %></td>
</tr>
<% end %>
</table>
</div>

As Robin said, that's invalid HTML. You probably shouldn't do that.
I personally would put an onclick event on the tr using jQuery. The tr element would look like this:
<tr data-link="<%= edit_scout_path(scout) %>">
...
</tr>
And then the associated JavaScript (placed in a file such as app/assets/javascripts/scouts.js) would be something like this:
$("tr[data-link]").click(function() {
window.location = $(this).data("link")
})
This would make all tr elements that have a data-link attribute act as if they were URLs in the most unobtrusive way I can think possible.

I am new on rails and I have the same problem and use the Ryan's advise with some changes that are following -
$("tr").click(function() {
window.location = $(this).data("link")
})
You have to use $(this).

Here is my take to make those links, remote: true
$("tr[data-link]").click(function () {
$.ajax({
url: this.getAttribute('data-link'),
dataType: "script",
type: "GET"
});
event.preventDefault();
});

Simple Solution: add a link into a table cell:
This doesn't answer your question, but it provides a solution to the problem you are likely really after: just add an edit link into a cell, rather than on the table row because having a link on the table row itself may lead to expected results for users. If they are clicking on it, they might not want to be led to an edit link.
Like my grandpa used to say: KISS - Keep It Simple Stupid
<% #scouts.each do |scout| %>
<tr>
<!-- Simply edit the scout -->
<td> <%= link_to edit_scout_path(scout), "Edit Scout" %> </td>
<td><%= scout.name %></td>
<td><%= scout.rank %></td>
<td><%= scout.advancement %></td>
<td><%= scout.age %></td>
</tr>

Related

.html.erb set div doesn't work correctly

I just created a scaffold in rails and list all stories in index page, and _stories.html.erb is partial which was rendered in index.html.erb
I want each story div has a red background for example:
.storyCell{
background-color:red;
height:100px;
}
_stories.html.erb
<tbody>
<% #stories.each do |story| %>
<div class="storyCell">
<tr>
<td><%= story.content %></td>
<td><%= story.finished %></td>
<td><%= link_to 'Show', story %></td>
<td><%= link_to 'Edit', edit_story_path(story) %></td>
<td><%= link_to 'Destroy', story, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
</div>
<% end %>
</tbody>
But the result is the red div all in top of the story model properties. Thank you!
This is invalid html. You can't have a div inside a tbody. Remove your div, and just put the class directly on the table row:
<tr class="storyCell">
What's happening here is that the browser is trying to do the best it can to render the invalid html, and so it pulls the div out (which it nows is not allowed inside the table) and renders it above the table instead.
You can give the class to <tbody> like this:
<tbody class="storyCell">
Here is the w3schools example you can look at: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_tbody
.You can't insert div in table elements as it will render in correct html output.

Rails create table row on button click

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.

Hiding some content on specific layout - Rails

I have two layouts - application.html.erb and application.pdf.erb
They both render the same partial invoices/show.html.erb
invoices/show.html.erb has a table and a part of it looks like this:
<tr>
<td><%= in.name%></td>
<td><%= link_to 'remove', some_path(in), method: :delete%></td>
</tr>
This is working fine but I would like to remove the link_to when I am rendering application.pdf.erb layout. So it looks like this:
<tr>
<td><%= in.name%></td>
<td></td>
</tr>
I have tried to do it by using content_for/yield but I can't have it working.
Any suggestion on how to achieve this?
You can actually get the format in your partial:
<tr>
<td><%= in.name%></td>
<% if controller.request.format == "html" %>
<td><%= link_to 'remove', some_path(in), method: :delete%></td>
<% end %>
</tr>

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.

checkbox_tag in a HTML table in rails app - using AJAX to update model

I'd like to make the following checkboxes and radiobuttons invoke the "update" method in my listings_controller
How do I make that happen? I've coded the elements there within my table but how can I have them send the parameters? I would like to use AJAX where do I begin?
<%= stylesheet_link_tag 'listings' %>
<h1>Listing listings</h1>
<table class="datatable">
<tr id="heading" >
<th >id</th>
<th >name</th>
</tr>
<% #listings.each do |listing| %>
<tr id="body">
<th><%=listing.id%></th>
<th><%= link_to listing.name, edit_listing_path(listing) %></th>
#How can I have the following invoke the update method in AJAX format
<td><%= radio_button_tag(:keep, "Keep") %>
<%= label_tag(:keep, "Keep") %></td>
<td><%= radio_button_tag(:keep, "Delete") %>
<%= label_tag(:keep, "Delete") %></td>
<td><%= check_box_tag(:checked) %>
<%= label_tag(:checked, "checked") %></td>
<td><%= check_box_tag(:collected) %>
<%= label_tag(:collected, "collected") %></td>
<td><%= check_box_tag(:digitized) %>
<%= label_tag(:digitized, "digitized") %></td>
<td><%= check_box_tag(:in_database) %>
<%= label_tag(:in_database, "in database") %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Listing', new_listing_path %>
Thanks a lot.
You're needs are pretty vague, but definitely use jQuery for this. Go into app/assets/javascripts/listings.js.coffee and try the following.
$('input.some_class').bind('update', ->
$.post('/listings/update', { param1: value, param2: value ... }, (data) ->
alert('updated!')
Or just plain javascript (app/assets/javascripts/listings.js)
$('input.some_class').bind('update', function() {
$.post('/listings/update', { param1: value, param2: value ... }, function(data) {
alert('updated!');
});
});
Give your checkboxes, radio buttons, etc some class name
Fill in the appropriate fields for the data you want to send (the data accepted by the updates method in your listings controller)
Put appropriate code for call back (rather than alert('updated!'), you could even put nothing at all)
But again, your needs are a little vague, I hope this is a good starting point. I'm going to recommend learning jQuery and maybe coffeescript before attempting to incorporate AJAX into your project. You could tweak it a little to utilize :remote => true as well, but that's another story.

Resources