【问题标题】:RAILS 4: problems with 'delete' buttonRAILS 4:“删除”按钮的问题
【发布时间】:2015-03-03 13:40:15
【问题描述】:

我有一个嵌套资源,像这样的 cmets....

resources :microposts do

   member do
     get :upvote, :undo_upvote
   end

   member do
     get :follow, :unfollow
   end

   resources :responses do
     member do
       get :upvote, :undo_upvote
     end
     resources :comments
  end
end

我在 cmets 索引页上有一个删除按钮....

 <div class = "Comment" id="comment-<%= comment.id %>">
     <%= link_to comment_avatar_for(comment.user), comment.user %>
     <span class="Commentator">
       <%= comment.user.full_name %>
     </span>
     <span class="content">
       <%= comment.body %>
     </span>
     <span class="timestamp">
     Posted <%= time_ago_in_words(comment.created_at) %> ago.
     </span>
     <span class="timestamp">
       <% if current_user?(comment.user) %>
       <%= link_to "delete", comment, method: :delete, data: { confirm: "You sure?" }, :class => "btn btn-default btn-xs delete" %>
      <% end %>
     </span>
</div>

我在加载页面时收到此错误

 undefined method `comment_path' for #<#  <Class:0x007f8936876e70>:0x007f8931857020>

我不确定为什么这不起作用 - 毕竟我有正确的“评论”实例。如果有人能指出我正确的方向,我将不胜感激。

【问题讨论】:

    标签: ruby-on-rails nested nested-resources


    【解决方案1】:

    Rails 做出假设。

    因为你有一个Comment 的实例,它假设你将使用comment_path,但你没有根据你的路线,所以你需要设置正确的路径:

    <%= link_to "delete", micropost_response_comment_path(@micropost, @response, comment), method: :delete, data: { confirm: "You sure?" }, :class => "btn btn-default btn-xs delete" %>
    

    我可能走错了路,但希望你能明白。

    【讨论】:

    • 如果想删除comment,OP 应该确定是否需要@micropost@response 资源。
    • 工作......虽然我对马格努斯的评论很感兴趣。我想他的意思是,只是具体的路线。
    • 是的,具体路线。由于您可能不关心删除评论的父资源。因此有类似:&lt;%= link_to "delete", comment_path(comment)...。但总而言之,没有什么比一条更短的路径更重要的了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-03
    相关资源
    最近更新 更多