删除 post 重定向到 post
Delete post redirect to the post
我目前正在尝试通过 link
删除 post
<%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' }
问题是,当我点击它时,它没有破坏 post,而是将我重定向到 post 的显示视图。
这是我的 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
感谢您的帮助
确保 //= require jquery
和 //= require jquery_ujs
包含在 application.js
中,并将 application.js
包含在 view/layout/application.html.erb
中
我目前正在尝试通过 link
删除 post<%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' }
问题是,当我点击它时,它没有破坏 post,而是将我重定向到 post 的显示视图。
这是我的 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
感谢您的帮助
确保 //= require jquery
和 //= require jquery_ujs
包含在 application.js
中,并将 application.js
包含在 view/layout/application.html.erb