【问题标题】:ROR: Delete action only reloading pageROR:仅删除操作重新加载页面
【发布时间】:2016-10-25 20:11:29
【问题描述】:

我相信我已经完全按照教程进行操作,但是当我单击“垃圾箱”链接时,页面会重新加载到相同的 @doc 页面,而没有任何更改。

查看代码,让我知道需要更改的内容。

DELETE 是rake routes 下列出的动词,用于docs#destroy

非常感谢您的帮助!

show.html.haml:

%h1= @doc.title
%p= @doc.content

= link_to "All Docs", docs_path
= link_to "Fix Docs", edit_doc_path(@doc)
= link_to "Trash It", doc_path(@doc), method: :delete, data: { confirm: "Are you Sure?" }

docs_controller.rb:

class DocsController < ApplicationController
    before_action :find_doc, only: [:show, :edit, :update, :destroy]

    def index
        @docs = Doc.all.order("created_at DESC")
    end

    def show
    end

    def new
        @doc = Doc.new
    end

    def create
        @doc = Doc.new(doc_params)

        if @doc.save
            redirect_to @doc
        else
            render 'new'
        end
    end

    def edit
    end

    def update
        if @doc.update(doc_params)
            redirect_to @doc
        else 
            render 'edit'
        end
    end

    def destroy
        @doc.destroy
        redirect_to docs_path
    end

    private 

        def find_doc
            @doc = Doc.find(params[:id])
        end

        def doc_params
            params.require(:doc).permit(:title, :content)
        end
end



Rails.application.routes.draw do
  get 'welcome/index'

  root 'welcome#index'

  resources :docs
end

【问题讨论】:

  • 请为这个控制器发布你的 routes.rb 配置。
  • Rails.application.routes.draw 确实得到 'welcome/index' 根 'welcome#index' 资源 :docs end
  • 您在什么环境下运行您的应用程序?开发/测试/产品?
  • 开发。它仅在本地运行。
  • 你用的是什么版本的rails?

标签: ruby-on-rails ruby ruby-on-rails-4


【解决方案1】:

确保 gem 'jquery-rails' 在 gemfile 中并且 jquery_ujs 包含在 app/assets/javascripts/application.js 文件中

//= require jquery
//= require jquery_ujs

我相信你的问题与this question类似。

【讨论】:

  • 感谢您的意见。不幸的是,这不是问题,因为上述问题已经存在。
【解决方案2】:

感谢大家的帮助!我能够找出问题的根源。

我在第 5 行和第 6 行的 application.html.erb 中将“application”更改为“default”,以修复 Pages#home 中的 Rails ExecJS::ProgramError? Windows 机器上常见的错误。

作为参考,修复方法是安装 gem 'coffee-script-source' 的上一版本,'1.8.0'。改回application.html.erb文件后,就开始营业了!

再次感谢所有帮助过的人!

【讨论】:

    【解决方案3】:

    页面上是否加载了 jquery_ujs(位于 https://github.com/rails/jquery-rails)?需要 Javascript 才能将 &lt;a&gt; 更改为带有 method=delete 的表单帖子。

    【讨论】:

    • 我愿意。我在 gem 文件中更新为 gem 'jquery-rails', '~> 4.2', '>= 4.2.1' 以确保。
    • 正确.. 但它是否加载到页面上?任何 javascript 错误?您可以在 chrome 调试器中查看网络请求,以确定单击链接时它是在执行 GET 还是 POST。
    • 这些显示在调试器的控制台选项卡中。这是您需要的信息吗?感谢您在与正在努力学习的人打交道时的耐心
    • 您需要网络选项卡,而不是控制台选项卡。当您单击删除链接时,请注意查看发出了哪些新请求。
    猜你喜欢
    • 2018-03-29
    • 2014-03-25
    • 2011-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多