2017-05-07 104 views
0

我目前正在試圖通過鏈接刪除帖子刪除後重定向到後

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

THX幫助

+3

您是否有'// = require'jquery'和'// = require'jquery_ujs'包含在'application.js'中?並且'view.js'包含在'view/layout/application.html.erb'中? –

+0

我只是添加// = require jquery_ujs。這是知道刪除正確謝謝老兄! – Che

+0

我已經添加了解決方案作爲答案。 –

回答

2

請確保您有//= require jquery//= require jquery_ujs包含在您的application.js中d包括application.js包含在view/layout/application.html.erb