Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 为什么我的删除/销毁方法和链接不起作用?_Ruby On Rails_Ruby_Destroy - Fatal编程技术网

Ruby on rails 为什么我的删除/销毁方法和链接不起作用?

Ruby on rails 为什么我的删除/销毁方法和链接不起作用?,ruby-on-rails,ruby,destroy,Ruby On Rails,Ruby,Destroy,在我的Ruby on Rails应用程序中,我在控制器中使用了销毁方法: before_action :set_film, only: [:show, :edit, :update, :destroy] def destroy @film = Film.find(params[:id]) @film.destroy redirect_to films_path end private def film_params params.require(:film)

在我的Ruby on Rails应用程序中,我在控制器中使用了销毁方法:

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

def destroy
    @film = Film.find(params[:id])
    @film.destroy
    redirect_to films_path
end
private

def film_params
    params.require(:film).permit(:title, :synopsis, :length, :director, :cast1, :cast2, :cast3, :release_date, :warnings, :certificate)
end

def set_film
  @film = Film.find(params[:id])
end
从films_控制器中的film_params获取参数:

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

def destroy
    @film = Film.find(params[:id])
    @film.destroy
    redirect_to films_path
end
private

def film_params
    params.require(:film).permit(:title, :synopsis, :length, :director, :cast1, :cast2, :cast3, :release_date, :warnings, :certificate)
end

def set_film
  @film = Film.find(params[:id])
end
我在index.html.erb页面中调用此方法:

<%= link_to 'Destroy', film_path(film), method: :delete, data: { confirm: 'Are you sure?' } %>
运行rake路由的输出:

 H:\Sites\ThorCinema\Under Construction\ThorCinema>rake routes
      Prefix Verb   URI Pattern                  Controller#Action
 films_index GET    /films/index(.:format)       films#index
       films GET    /films(.:format)             films#index
             POST   /films(.:format)             films#create
    new_film GET    /films/new(.:format)         films#new
   edit_film GET    /films/:id/edit(.:format)    films#edit
        film GET    /films/:id(.:format)         films#show
             PATCH  /films/:id(.:format)         films#update
             PUT    /films/:id(.:format)         films#update
             DELETE /films/:id(.:format)         films#destroy
    showings GET    /showings(.:format)          showings#index
             POST   /showings(.:format)          showings#create
 new_showing GET    /showings/new(.:format)      showings#new
edit_showing GET    /showings/:id/edit(.:format) showings#edit
     showing GET    /showings/:id(.:format)      showings#show
             PATCH  /showings/:id(.:format)      showings#update
             PUT    /showings/:id(.:format)      showings#update
             DELETE /showings/:id(.:format)      showings#destroy
        root GET    /                            films#index

有人能告诉我我做错了什么吗?我已经完全遵循了删除操作,但无法使删除生效。请某人帮忙,这仍然不起作用。

将链接的方法更改为
:删除
需要JavaScript

  • 确保在
    应用程序.js
    中需要
    jquery\u ujs

    //= require jquery_ujs
    
  • 确保浏览器中已启用JavaScript


  • 最终解决了这个问题,在application.html.erb文件中,我缺少以下几行:

      <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
      <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
      <%= csrf_meta_tags %>
    
    true%>
    正确%>
    

    感谢所有试图帮助您的人

    您的路线设置好了吗?你从
    rake routes | grep destroy | grep film
    中得到了什么?我已经添加了我的路线。如何显示rake路由的输出?您可以转到应用程序目录中的终端,然后在那里运行带有
    rake路由的命令,并将输出粘贴到这里。我无法从rails cmd复制和粘贴,尽管您可能可以。您使用的是什么操作系统?Ie windows、mac、linux?我的application.js中已经有了,我使用的是Firefox,所以假设JavaScript已经启用了。我从来没有使用JavaScript来拥有删除链接。这是rails最新版本的一个变化吗?@MaxWilliams就我记忆所及,它一直是这样的。我所知道的唯一变化是,由于Rails 3,它使用了不引人注目的JS(它输出
    数据方法
    属性,而不是丑陋的
    onclick
    处理程序)。@MaxWilliams-这是Rails中包含的javascript。链接只能创建GET请求,因此rails必须调整url以包含piggy backed
    \u method
    param。没有它,这只是一个常见的GET请求。当这个过程通过时,它仍然是GET,但是rails会将其视为DELETE@MaxWilliams也许您正在考虑使用
    按钮\u to
    ——它将直接将表单生成到标记中,而不需要JS。