Pins中的NoMethodError #index,未定义的方法`stringify_keys

时间:2016-11-18 16:55:11

标签: ruby-on-rails ruby

我不知道为什么我收到此错误。肯定有错误,但不幸的是我现在找不到它。

Index.html.erb:

<div id="pins" class="transitions-enabled">
  <% @pins.each do |pin| %>
    <div class="box panel panel-default">
      <%= link_to image_tag(pin.image.url(:medium)), pin %>
      <div class="panel-body">
        <p><%= pin.description %></p>
        <p><strong><%= pin.user.email if pin.user %></strong><p>

        <% if pin.user == current_user %>
          <div class="actions">
          <%= link_to edit_pin_path(pin) do %>
            <span class="glyphicon glyphicon-edit"></span>
            Edit
            <% end %>
            <%= link_to 'Delete', pin, method: :delete, data: { confirm: 'Are you sure?' } do %>
            <span class="glyphicon glyphicon-trash"></span>
            Delete
            <% end %>
          </div>
        <% end %>
      </div>
    </div>
  <% end %>
</div>

在我的控制台中,错误:

ActionView::Template::Error (undefined method `stringify_keys' for #<Pin:0x007f568d39a428>):
    12:             <span class="glyphicon glyphicon-edit"></span>
    13:             Edit
    14:             <% end %>
    15:             <%= link_to 'Delete', pin, method: :delete, data: { confirm: 'Are you sure?' } do %>
    16:             <span class="glyphicon glyphicon-trash"></span>
    17:             Delete
    18:             <% end %>

似乎我编辑了`

<%= link_to 'Delete', pin, method: :delete, data: { confirm: 'Are you sure?' } do %>`

<%= link_to pin, method: :delete, data: { confirm: 'Are you sure?' } do %>

它可以工作并且页面加载,但是会产生其他我之前没有的问题,例如当用户登录时没有在我的''''上加载'delete'选项。但是,当没有人出现时,它们会显示出来已登录:/

1 个答案:

答案 0 :(得分:0)

之后使用块编写link_to时,不需要链接的文本内容。

<%= link_to pin, method: :delete, data: { confirm: 'Are you sure?' } do %>
   <span class="glyphicon glyphicon-trash"></span>
   Delete
<% end %>

在传入link_to 'delete', pin, ... do时代码中,它尝试解析引脚作为选项哈希的一部分

至于您的删除按钮仅在有人未登录时显示,我最好的猜测是pin.usernil,当没有人登录时会匹配。所以可能会更新条件是if current_user.present? && pin.user == current_user