Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 辅助对象中的多个链接赢得';我没有出现_Ruby On Rails_View Helpers - Fatal编程技术网

Ruby on rails 辅助对象中的多个链接赢得';我没有出现

Ruby on rails 辅助对象中的多个链接赢得';我没有出现,ruby-on-rails,view-helpers,Ruby On Rails,View Helpers,我正在尝试创建帮助程序来清理我的视图。我创建一个只允许帖子所有者编辑或删除它 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',

我正在尝试创建帮助程序来清理我的视图。我创建一个只允许帖子所有者编辑或删除它

 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 %>

评论:

提交人:

最近的评论:
@

但是,仅显示“删除”链接。有没有一种方法可以在不创建其他方法的情况下显示两个链接


提前谢谢。

这种事情最好在局部处理。您需要的是视图目录中的另一个文件,如下所示:

def belongs_to_current_user rec
  current_user == rec.user
end
_编辑_links.html.erb

<%= link_to 'Edit', edit_conversation_path(conversation), class: 'btn btn-primary btn-sm '%>
<%= link_to 'Delete', conversation, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-primary btn-sm' %>

我解决了这个问题。我还不能发布答案,因为我是一个彻头彻尾的n00b。你的解决方案比我的好。非常感谢。
def belongs_to_current_user rec
  current_user == rec.user
end