Shadid
Shadid

Reputation: 4272

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 %>

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

Upvotes: 0

Views: 324

Answers (1)

Nicolas Maloeuvre
Nicolas Maloeuvre

Reputation: 3169

The second argument of link_to is a path

micropost_comment is not a path, you should replace it with 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

Upvotes: 2

Related Questions