使用方法删除RAILS:删除

时间:2015-05-25 14:50:26

标签: javascript ruby-on-rails

我是rails的新手并遵循教程https://mackenziechild.me/12-in-12/12/。我在删除应用中的帖子时遇到问题[rails应用允许创建,编辑,更新,销毁 帖子 (其中包含标题,链接和说明)] 。单击 销毁 按钮,而不是重定向到 主页 页面时, DELETION确认消息未显示 ,我认为它会不断重新加载页面。另外我注意到每次点击销毁按钮时帖子的 id 都会改变。当我检查主页(列出所有帖子)时,计数保持不变,没有删除任何内容。
仅供参考,我附上了show.html.haml的代码片段



%h1 Inspirations (SHOW)

%h1= @post.title
%p= @post.link
%p= @post.description

%p= @post.__id__

= link_to "Edit", edit_post_path(@post)
= link_to "Destroy", post_path(@post), method: :delete, data: { confirm: "Are you sure?"}
= link_to "Home", root_path




我的../app/views/layouts/application.html.erb文件如下



<!DOCTYPE html>
<html>
<head>
  <title>Muse</title>
  <%= stylesheet_link_tag    'default', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'default', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
&#13;
&#13;
&#13;

我的../app/assets/javascripts/application.js如下

&#13;
&#13;
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
&#13;
&#13;
&#13;

删除控制器中的方法代码

&#13;
&#13;
class PostsController < ApplicationController

  before_action :find_post, only: [:show, :edit, :update, :destroy]

  ....
                                             
  def destroy
    @post.destroy
    redirect_to root_path
  end

  private

  def find_post
    @post = Post.find(params[:id])
  end

  def post_params
    params.require(:post).permit(:title, :link, :description)
  end
                                             
&#13;
&#13;
&#13;

我的一些观察


删除前我的主页 enter image description here


删除虚拟帖子
enter image description here


Dummy Post的ID在点击Destroy按钮时发生了变化 enter image description here


但是在检查主页(删除后)时,仍会列出所有帖子

请在我出错的地方帮助我,提前谢谢:)

1 个答案:

答案 0 :(得分:0)

您正在尝试加载default.js,而javascript_link_tag并不存在。您对 javascript_link_tag "application", "data-turbolinks-track" => true 的来电实际上应该是

stylesheet_link_tag

您对{{1}}的来电同样错误,尽管这与您的问题无关。