这是我的视图文件的代码,它显示了表的多个列。
限制正在进行,但列数是多个。
<table class="table table-responsive">
<tr>
<th>Title</th>
<th>Description</th>
<% obj.each do |article| %>
<% if logged_in? && current_user == article.user %>
<th>Edit</th>
<th>Show</th>
<th>Delete</th>
<th>Created By</th>
<th>Created At</th>
<th>Updated At</th>
<% end %>
<% end %>
</tr>
<% obj.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.description %></td>
<% if logged_in? && current_user == article.user %>
<td><%= link_to "Edit", edit_article_path(article), class: "btn btn-primary" %> </td>
<td><%= link_to "show", article_path(article), class: "btn btn-success" %></td>
<td><%= link_to "Delete", article_path(article), method: :delete, data: {confirm: "Are you Sure?"}, class: "btn btn-danger" %></td>
<td> <%= article.user.username if article.user %> </td>
<td> <%= time_ago_in_words(article.created_at) %> ago.</td>
<td> <%= time_ago_in_words(article.updated_at) %> ago.</td>
</tr>
<% end %>
<% end %>
</table>
<%= link_to 'Back', root_path, class: "btn btn-primary btn-lg" %>
答案 0 :(得分:2)
根据thead
的值打印obj
,尝试只留下第二次迭代,打印每个tr,可能是这样的:
<% if logged_in? && current_user == article.user %>
<tr>
<th>Edit</th>
<th>Show</th>
<th>Delete</th>
<th>Created By</th>
<th>Created At</th>
<th>Updated At</th>
</tr>
<tbody>
<% obj.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.description %></td>
<% if logged_in? && current_user == article.user %>
<td><%= link_to "Edit", edit_article_path(article), class: "btn btn-primary" %> </td>
<td><%= link_to "show", article_path(article), class: "btn btn-success" %></td>
<td><%= link_to "Delete", article_path(article), method: :delete, data: {confirm: "Are you Sure?"}, class: "btn btn-danger" %></td>
<td> <%= article.user.username if article.user %> </td>
<td> <%= time_ago_in_words(article.created_at) %> ago.</td>
<td> <%= time_ago_in_words(article.updated_at) %> ago.</td>
<% end %>
</tr>
<% end %>
</tbody>
<% end %>
答案 1 :(得分:2)
我对您的标题问题的理解是,您希望阻止查看所有表格。所以
<% if logged_in? && current_user == article.user %>
<table class="table table-responsive">
<tr>
...
the rest of the code for show the table
....
</table>
<% end %>
这将隐藏整个表的视图,它只会显示给文章作者
答案 2 :(得分:1)
问题是,您正在迭代article
中的每个obj
,并在每个时间输出标题当前登录用户是文章用户:
<% obj.each do |article| %>
<% if logged_in? && current_user == article.user %>
<th>Edit</th>
<th>Show</th>
<th>Delete</th>
<th>Created By</th>
<th>Created At</th>
<th>Updated At</th>
<% end %>
<% end %>
如果用户已登录并且是任何 article
的用户,您可能只想显示一次标题,Ruby有一个方法,{{3} }。所以这样的事情应该是你正在寻找的东西:
<% if logged_in? && obj.any? { |article| current_user == article.user } %>
<th>Edit</th>
<th>Show</th>
<th>Delete</th>
<th>Created By</th>
<th>Created At</th>
<th>Updated At</th>
<% end %>
如果用户已登录并且是页面上任何文章的作者,则应该只输出一组标题,而不是每篇文章创作的一组标题。