ROR:删除仅重新加载页面的操作

时间:2016-10-25 20:11:30

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

我相信我已经完全遵循了教程,但是当我点击“删除它”链接时,页面会重新加载到相同的@doc页面而不做任何更改。

查看代码并告诉我需要更改的内容。

DELETE是rake routesdocs#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

3 个答案:

答案 0 :(得分:0)

您是否在页面上加载了jquery_ujs(在https://github.com/rails/jquery-rails处找到)?需要使用Javascript将<a>更改为使用method = delete的表单帖子。

答案 1 :(得分:0)

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

//= require jquery
//= require jquery_ujs

我认为您的问题类似于this question

答案 2 :(得分:0)

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

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

作为参考,修复方法是安装gem'coffee-script-source'的前身版本,'1.8.0'。更改了application.html.erb文件后,我现在正在开展业务!

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