【问题标题】:Delete post redirect to the post删除帖子重定向到帖子
【发布时间】:2017-10-05 16:27:58
【问题描述】:

我目前正在尝试通过链接删除帖子

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

感谢您的帮助

【问题讨论】:

  • 您的application.js 中是否包含//= require jquery 和//= require jquery_ujs? application.js 是否包含在 view/layout/application.html.erb 中?
  • 我只是添加 //= 需要 jquery_ujs。它知道正确删除谢谢老兄!
  • 我已添加解决方案作为答案。

标签: ruby-on-rails ruby link-to posts


【解决方案1】:

确保您的application.js 中包含//= require jquery 和//= require jquery_ujs 并将application.js 包含在view/layout/application.html.erb 中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多