2016-08-03 93 views
0

我有一个部分,从一个数据库的列迭代它,并显示在网页上的所有网址。我想添加我写在我的app/views/topics/show.html.erb页面中的行,允许用户编辑,删除或喜欢链接。试图适应代码形式我的意见之一是在部分呈现

这些是我想适应添加到部分的三条线,前两条是执行编辑和删除功能的按钮,第三条是使用类似代码的部分链接。

<%= link_to "Edit bookmark", edit_topic_bookmark_path(bookmark.topic, bookmark), class: 'btn btn-primary btn-xs'%><br> 
<%= link_to "Delete bookmark", [bookmark.topic, bookmark], method: :delete, class: 'btn btn-danger btn-xs', data: { confirm: 'Are you sure you want to delete this bookmark?' } %> 
<%= render partial: 'likes/likes', locals: { bookmark: bookmark } %> 

这部分我想它适应位于app/views/bookmarks/_bookmarksandlikes.html.erb

<div> 
    <h3> 
     <%y=1%> 
     <% mark.each do |x| %> 
      <%=y%>)<%= link_to x.url, x.url%> 
      <%y=y+1%> 

     <% end %> 
    </h3> 
</div> 

,它正在从app/views/users/show.html.erb与这两条线叫。

<%= render partial: 'bookmarks/bookmarksandlikes', locals: { mark: @bookmarks} %> 
<%= render partial: 'bookmarks/bookmarksandlikes', locals: { mark: @liked_bookmarks} %> 

这里就是我试图将代码插入到部分

<div> 
    <h3> 
    <%y=1%> 
    <% mark.each do |x| %> 
     <%=y%>)<%= link_to x.url, x.url%> 
     <%y=y+1%> 
     <%= link_to "Edit bookmark", edit_topic_bookmark_path(x.topic, x), class: 'btn btn-primary btn-xs'%><br> 
     <%= link_to "Delete bookmark", [x.topic, x], method: :delete, class: 'btn btn-danger btn-xs', data: { confirm: 'Are you sure you want to delete this bookmark?' } %> 
     <%= render partial: 'likes/likes', locals: { x: x } %> 
    <%end%> 
</h3> 

当我运行的代码是我得到一个错误页,上面写着“undefined local variable or method 'bookmark' for #<#<Class:0x007fdfb1b39c80>:0x007fdfb1988d50> Did you mean? bookmark_url or @bookmarks

并且说第3行有部分代码喜欢页面出现问题app/views/likes/_likes.html.erb

<% if policy(Like.new).create? %> 
    <div> 
    <% if like = current_user.liked(bookmark) %> 
    <%= link_to [bookmark, like], class: 'btn btn-danger', method: :delete do %> 
     <i class="glyphicon glyphicon-star-empty"></i>&nbsp; Unlike 
    <% end %> 
<% else %> 
    <%= link_to [bookmark, Like.new], class: 'btn btn-primary', method: :post do %> 
    <i class="glyphicon glyphicon-star"></i>&nbsp; Like 
    <% end %> 
<% end %> 
</div> 
<% end %> 

注意在这一点上通过这个问题的工作,似乎只有第三行为部分我试图插入给我的问题,编辑和删除按钮渲染很好。

即使你不知道答案我很想听听我对这个问题或者你的想法没有考虑的任何想法,如果你需要更多的信息,请让我知道,我会发布更多。

谢谢你看我的问题。

+2

那么你遇到了什么问题? – Shaunak

+0

我得到一个错误消息,说:“未定义的局部变量或方法书签为#<#:0x007fdfb1c67ee0>你的意思是吗?bookmark_url或@书签' 也感谢你问,它使我回去检查一些事情。 – SinGar

回答

0

我想通了,我的问题,我不能使用的部分,因为该部分app/views/likes/_likes.html.erb正在采取比正被app/views/bookmarks/_bookmarksandlikes.html.erb通过不同的变量,所以我只是把需要的代码放到app/views/bookmarks/_bookmarksandlikes.html.erb和适应它像这样。

<div class=row> 
    <h3> 
     <%y=1%> 
     <% mark.each do |x| %> 
     <%=y%>)<%= link_to x.url, x.url%><br> 
     <%y=y+1%> 
     <%= link_to "Edit bookmark", edit_topic_bookmark_path(x.topic, x), class: 'btn btn-primary btn-xs'%><br> 
     <%= link_to "Delete bookmark", [x.topic, x], method: :delete, class: 'btn btn-danger btn-xs', data: { confirm: 'Are you sure you want to delete this bookmark?' } %><br> 
     <% if like = current_user.liked(x) %> 
     <%= link_to [x, like], class: 'btn btn-danger', method: :delete do %> 
     <i class="glyphicon glyphicon-star-empty"></i>&nbsp; Unlike 
     <% end %> 
    <% else %> 
     <%= link_to [x, Like.new], class: 'btn btn-primary', method: :post do %> 
     <i class="glyphicon glyphicon-star"></i>&nbsp; Like 
     <% end %> 
     <br> 
     <% end %> 
     <%end%> 

    </h3> 
    </div> 
相关问题