Rails 路由错误(未定义的局部变量或方法 `micropost_comment')

Rails Routing Error (undefined local variable or method `micropost_comment')

NameError in StaticPages#home I don't quite understand why. Here my code from views/comments/_comment.html.erb

<% if current_user?(comment.user) %>

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

<% end %>

这是我的路线

 micropost_comment GET    /microposts/:micropost_id/comments/:id(.:format)      comments#show
                       PATCH  /microposts/:micropost_id/comments/:id(.:format)      comments#update
                       PUT    /microposts/:micropost_id/comments/:id(.:format)      comments#update
                       DELETE /microposts/:micropost_id/comments/:id(.:format)      comments#destroy

谁能解释一下如何解决这个错误

undefined local variable or method `micropost_comment'

提前致谢

link_to的第二个参数是路径

micropost_comment 不是路径,您应该将其替换为 micropost_comment_path

还有关于你的路线,你应该添加那些参数:

<%= link_to 'Destroy', micropost_comment_path(micropost_id:comment.micropost_id, id:comment.id), method: :delete, data: { confirm: 'Are you sure?' } %>

虽然我认为你不需要嵌套路由,但这是另一个问题