无法在Rails中调用Delete方法

时间:2016-02-19 14:51:04

标签: ruby-on-rails ruby

所以我在我的视图中实现了删除用户方法,这是我的users_controller中的destroy方法

def destroy
@user = User.find(params[:id])
@user.destroy
respond_to do |format|
  format.html { redirect_to users_path, notice: 'This user was successfully destroyed.' }
  format.json { head :no_content }
end
end

我的观点:

<td><%= link_to 'Delete', users_path(user), method: :delete, data: { confirm: 'Are you sure?' } %></td>

和application.js

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require bootstrap-sprockets
//= require_tree .

在layouts / application.html.erb中我有:

<%= javascript_include_tag 'default' %>

每次我点击销毁链接时,它都会刷新并且不会删除。我应该提到的是视图在superadmins下,superadmins_controller继承自users_controller,我写了destroy方法。我认为route.rb可能有问题,但我无法想出解决方案。我的route.rb看起来像这样:

Rails.application.routes.draw do
root to: 'visitors#index'
devise_for :users, :path =>'u'
devise_scope :user do
    get '/u/sign_out' => 'devise/sessions#destroy'
  end
  resources :users
  resources :superadmins
end

谢谢!

在控制台上更新

    ActionController::RoutingError (No route matches [GET] "/javascripts/default.js"):
  actionpack (4.2.5.1) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  web-console (2.3.0) lib/web_console/middleware.rb:28:in `block in call'
  web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'
  web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.5.1) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.5.1) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.5.1) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.5.1) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.5.1) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.5.1) lib/rails/rack/logger.rb:20:in `call'
  quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.4) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.5.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/static.rb:116:in `call'
  rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
  railties (4.2.5.1) lib/rails/engine.rb:518:in `call'
  railties (4.2.5.1) lib/rails/application.rb:165:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  rack (1.6.4) lib/rack/content_length.rb:15:in `call'
  rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
  /Users/pc/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
  /Users/pc/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
  /Users/pc/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'

3 个答案:

答案 0 :(得分:2)

尝试改变: <%= javascript_include_tag 'default' %> 致:<%= javascript_include_tag 'application' %> 因为在您的控制台中,第一个错误是ActionController::RoutingError (No route matches [GET] "/javascripts/default.js")

答案 1 :(得分:0)

查看您的路线定义和您的link_to帮助程序(指定method: :delete。尝试更改

get '/u/sign_out' => 'devise/sessions#destroy'

delete '/u/sign_out' => 'devise/sessions#destroy'

答案 2 :(得分:0)

我认为您的问题是您使用users_path而不是正确的user_path

所以改变这个

<td><%= link_to 'Delete', users_path(user), method: :delete, data: { confirm: 'Are you sure?' } %></td>

到这个

<td><%= link_to 'Delete', user_path(user), method: :delete, data: { confirm: 'Are you sure?' } %></td>