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.
Related
Can anyone tell me why the email addresses are not showing in the users list view below?
<p id="notice"><%= notice %></p>
<h1>Users</h1>
<table>
<thead>
<tr>
<th>Email</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #users.each do |user| %>
<tr>
**<td><%= user.email %></td>**
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New User', new_user_path %>
I'm not familiar with seeing the two asterisks around the element. I'm thinking that might be messing something up. If you want to bold the user's email you could try something like <td><b><%= user.email %></b></td>.
Are you getting any errors anywhere?
If the asterisks are just there to help us see the line of code, and you say that you see the users (and their emails) in the db, I think it could be one of two other relatively minor things.
Whats your controller look like for this view? Are you passing #users to this view? You could try just dumping #users on the page to make sure that it's available.
Is the field on your User model that holds the email called email?
I'm using tables to list recorded from database in index templates, And as usual the last three table cells are used for the links of Show, Edit and Destroy the Object.
../users/index.html.erb
<table>
...
<% #users.each do |user| %>
<tr>
...
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
Anyway, I was trying to replace those links text to some Bootstrap's glyph icons, And I succeeded but It got messy so I thought it better to put it in a partial with a variable to be shared with all index templates that use the same table layout.
../shared/_editLinks.html.erb
<td>
<%= link_to dist do %>
<span class="glyphicon glyphicon-eye-open"></span>
<% end %>
</td>
<td>
<%= link_to send("edit_#{dist}_path(#{dist})") do %>
<span class="glyphicon glyphicon-edit"></span>
<% end %>
</td>
<td>
<%= link_to dist, method: :delete, data: { confirm: 'Are you sure?' } do %>
<span class="glyphicon glyphicon-remove"></span>
<% end %>
</td>
Then To use the following code line to render the partial in the index table. Passing the resource name as variable.
<%= render 'editLinks', dist: user %>
Then the first and the last links seems to work fine but I got this error around the middle -Edit- link.
undefined method `edit_user_path(#<User:0x007f611ab015a8>)' for #<#<Class:0x007f611a7064d0>:0x007f611ab143b0>
Can you tell me what causes this error and how to get it work?
The lines causing errors are because you're trying to treat an object like a string.
Because the _path helpers are typically snake_case, you can use the underscore method on the object's class name like so:
<%= link_to send("edit_#{dist.class.name.underscore}_path", dist) do %>
As pointed out by Deepak, you also can be providing the dist object as the second argument to send. Otherwise, you'll end up with a similar error because you'd again be treating the object as a value that can be coerced into a string.
Pass param to route/method separated by comma
<%= link_to send("edit_#{dist.class.name.underscore}_path", dist) do %>
send method syntax
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>
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>
I have an application where I want to render the menu using "content_for" as well as a left navigation bar and then the rest be the body of my document. However, in my left bar and menu regions, the html markup is being escaped. I saw a post regarding using raw before the yield, but that didn't help.
After spending quite some time trying to diagnose it within the real app, I created a VERY simple test and was able to get it to do the same with minimal code. Here is what I have:
layouts.application.erb
<body>
<section id="page">
<header>
<nav class="clear"><%= raw yield :navigation %></nav>
</header>
<%= yield %>
</section>
</body>
</html>
pages/index.html.erb
<% content_for :navigation do %>
<ul><li><%= link_to 'New page', new_page_path %></li></ul>
<% end %>
<h1>Listing pages</h1>
<table>
<tr>
<th>Title</th>
<th>Body</th>
</tr>
<% #pages.each do |page| %>
<tr>
<td><%=h page.title %></td>
<td><%=h page.body %></td>
<td><%= link_to 'Show', page %></td>
<td><%= link_to 'Edit', edit_page_path(page) %></td>
<td><%= link_to 'Destroy', page, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
When I hit the page in a browser, the navigation part is not escaped properly (actually the link is, but not the list, which is kinda important with menus:) )
This is from a brand new rails project using scaffold generated code which was modified only to move the default link up into the navigation section (and adding that piece in).
The escaping is happening when you set aside the content, not when you display it. Try using raw within content_for:
<% content_for raw :navigation do %>