我正在尝试创建助手来清理我的视图。我创建一个只允许帖子的所有者编辑或删除它。
module ConversationsHelper
def editing_for_current_user (convo)
if current_user == convo.user
link_to 'Edit', edit_conversation_path(convo), class: 'btn btn-primary btn-sm'
link_to 'Delete', convo, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-primary btn-sm'
end
end
end
这是我的看法:
<div class="row">
<% @conversations.each do |conversation| %>
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<h3><%= link_to conversation.title, conversation %></h2>
<p>Comments: <span class='badge'><%= conversation.comments.count %></span></p>
<p><%= conversation.description %></p>
<p><b>Submitted by:</b> <%= link_to conversation.user.username, user_path(conversation.user.id) %></p>
<h3> Recent comments: </h3>
<hr>
<% conversation.comments.sort_by{|t| - t.created_at.to_i}.take(3).each do |c| %>
<div class='media'>
<%= image_tag c.user.profile_pic.url(:thumb), class: 'media-object pull-left img-polaroid' %>
<div class='media-body'>
<h4 class='media-heading'><%= link_to c.user.username, user_path(c.user.id) %><small> @ <%=c.created_at.strftime("%m/%d/%Y at %I:%M%p") %></small></h4>
<%= simple_format(c.message) %>
</div>
</div>
<hr>
<% end %>
<%= editing_for_current_user(conversation) %>
</div>
</div>
<% end %>
但是,仅显示“删除”链接。有没有办法在不创建其他方法的情况下同时显示 link_to?
提前致谢。