简体   繁体   English

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

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

NameError in StaticPages#home I don't quite understand why. 我不太清楚为什么在StaticPages#home中的NameError。 Here my code from views/comments/_comment.html.erb 这是我来自views/comments/_comment.html.erb代码

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

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

<% end %>

Here's my routes 这是我的路线

 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

can some one explain how to fix this error 有人可以解释如何解决此错误

undefined local variable or method `micropost_comment'

thank you in advance 先感谢您

The second argument of link_to is a path link_to的第二个参数是路径

micropost_comment is not a path, you should replace it with micropost_comment_path micropost_comment不是路径,应将其替换为micropost_comment_path

also regarding to you routes, you should add those params : 对于您的路线,还应添加以下参数:

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

I think you don't need nesting routes though, but it's an other question 我认为您虽然不需要嵌套路由,但这是另一个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails:路由错误,注释模型到微博模型 - Rails: Routing Error, comment model to micropost model M. Hartl的Ruby on Rails教程。 第10.2.1章 - 未定义的局部变量或方法`micropost&#39; - Ruby on Rails Tutorial by M. Hartl. Chapter 10.2.1 - undefined local variable or method `micropost' Rails教程第2章错误noMethodError:未定义的方法&#39;micropost&#39; - rails tutorial ch 2 error noMethodError: undefined method 'micropost' 当我尝试为微博创建评论时,出现“未定义的方法&#39;comment_content&#39;”错误 - When I tried to create comment to micropost I have an error as “undefined method `comment_content'” Rails 4路由-未定义的局部变量 - Rails 4 Routing - undefined local variable Rails 6 Route Error未定义的局部变量或方法 - Rails 6 Route Error undefined local variable or method Rails的未定义局部变量或方法错误 - undefined local variable or method error with rails Rails路由错误未定义的局部变量或方法 - rails route error undefined local variable or method Ruby on Rails中未定义的局部变量/无方法错误 - Undefined local variable / no method error in Ruby on Rails 主对象的路由错误未定义局部变量或方法“ applicationController” - Routing Error undefined local variable or method `applicationController' for main:Object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM