我目前正试图通过链接删除帖子
<%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' }
问题在于,当我点击它时,它不会破坏帖子,而是将我重定向到帖子的节目视图。
这是我的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>
这是我的控制器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
帮助