我想在rails应用程序中创建按钮Destroy,但它会显示注释
undefined method `comment_path' for #<#<Class:0x007fe0566264c8>:0x007fe05a5df3f8>
Did you mean? font_path
虽然我没有font_path,但当我摆脱这个伪代码时,问题就消失了,但我不能破坏评论。
<% if comment.user == current_user -%>
<%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-sm btn-default" %>
<% end %>
如果我摆脱这个问题,问题就消失了,但我无法删除评论。如果我在'Destroy'中将@符号放在评论之前,那么这不是将被删除的评论,而是帖子。这是完整的代码:
<%= div_for(comment) do %>
<div class="comments_wrapper clearfix">
<div class="pull-left">
<p class="lead"><%= comment.body %></p>
<p><small>Submitted <strong><%= time_ago_in_words(comment.created_at) %> ago</strong> by <%= comment.user.email %></small></p>
</div>
<div class="btn-group pull-right">
<% if comment.user == current_user -%>
<%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-sm btn-default" %>
<% end %>
</div>
</div>
这是我的routes.rb代码
Rails.application.routes.draw do
devise_for :users
resources :links do
member do
put "like", to: "links#upvote"
put "dislike", to: "links#downvote"
end
resources :comments
end
root to: "links#index"
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
# root 'welcome#index'
# Example of regular route:
# get 'products/:id' => 'catalog#view'
# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Example resource route with more complex sub-resources:
# resources :products do
# # resources :sales do
# get 'recent', on: :collection
# end
# end
# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable
# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
end
感谢您的帮助!
答案 0 :(得分:1)
这是因为
<%= link_to 'Destroy', comment, ...
使Rails期望存在comment_path
辅助方法。但是您已将资源comments
嵌套在links
中。
所以这可能对你有帮助
<%= link_to 'Destroy', link_comment_path(link_id: comment.link_id, id: comment.id), method: :delete,...
这个答案可能不是最好的答案;但它应该工作。让我们沟通
答案 1 :(得分:0)
您的评论路径嵌套在链接下。您必须以下列方式访问它:
<% if comment.user == current_user -%>
<%= link_to 'Destroy',
[@link, comment],
method: :delete,
data: { confirm: 'Are you sure?' },
class: "btn btn-sm btn-default"
%>
<% end %>