Che Che - 2 months ago 13
Ruby Question

Delete post redirect to the post

I'm currently trying to delete a post through the link

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


The problem is that when i click on it, instead of destroying the post, it's redirect me to the show view of the post.

Here is my index.html.erb

<table>
<tr>
<th>titre</th>
<th>description</th>
</tr>
<% @posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.description %></td>
<td><%= link_to 'Show', post_path(post)%> </td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<br>




And here is my controller posts.controller

class PostsController < ApplicationController

before_action :set_post, except: [:create, :index, :new]
def index
@posts = Post.all
end

def show
end

def new
@post = Post.new
end

def edit
end

def create
@post = Post.new(post_params)
@post.save
redirect_to :action => "index"
end

def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Lime was successfully updated.' }
else
format.html { render :edit }
end
end
end

def destroy
@post.destroy
redirect_to root_path, :notice => "Vous n'ĂȘtes plus un batard"
end

private #private has nos end

#Must be set to update later
def set_post
@post = Post.find(params[:id])
end

def post_params
params.require(:post).permit(:title, :description)
end
end


Thx for helping

Answer

Make sure you have //= require jquery and //= require jquery_ujs included in your application.js And include application.js included into view/layout/application.html.erb