i want to approve the comments made

Post / Update actions require forms

You’re using a link_to. This is good for GET requests, but is no good for POST/PATCH/UPDATE requests. For that you’ll have to use a form in HTML. Luckily Rails offers some short cut. You can use something like button_to:

<%= button_to "Approve", { controller: "comments", action: "update" }, remote: false, form: { "id" => @comment.id, "approved" => true } %> 

This creates a form for you. Which will come with CSRF protection automatically. You can style the button however you like.

or you could use a link to:

 <%= link_to comment_approved_path(@comment), method: :put %>

but then you would need to create a separate “approved” action in your controller, and a separate route to reach it.

(The above code has not been tested).

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top