销毁记录会返回内部500错误

时间:2015-11-16 12:28:12

标签: ruby-on-rails

我的movies_controller.rb rails控制器中有一个destroy函数,

def destroy
  @movie = Movie.find params[:id]
  current_user.movies.destroy @movie
end

当用户销毁电影时我得到500 internal server error,但该记录会被删除。

这是我的rails服务器中的响应,

Started DELETE "/movies/49.json" for 127.0.0.1 at 2015-11-16 13:20:26 +0100
Processing by MoviesController#destroy as JSON
  Parameters: {"id"=>"49"}
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 5]]
  Movie Load (0.1ms)  SELECT  "movies".* FROM "movies" WHERE "movies"."id" = ? LIMIT 1  [["id", 49]]
   (0.0ms)  begin transaction
  HABTM_Movies Load (0.2ms)  SELECT "movies_users".* FROM "movies_users" WHERE "movies_users"."user_id" = ? AND "movies_users"."movie_id" = 49  [["user_id", 5]]
  SQL (0.2ms)  DELETE FROM "movies_users" WHERE "movies_users"."user_id" = ? AND "movies_users"."movie_id" = 49  [["user_id", 5]]
   (11.6ms)  commit transaction
Completed 500 Internal Server Error in 26ms (ActiveRecord: 12.2ms)

ActionView::MissingTemplate (Missing template movies/destroy, application/destroy with {:locale=>[:en], :formats=>[:json], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :haml, :jbuilder]}. Searched in:
  * "/home/alucardu/sites/movieseat/app/views"
  * "/home/alucardu/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/devise-3.5.2/app/views"
):

我已尝试在destroy.html.erb文件夹中添加空views/movies文件,但错误仍然存​​在。如果我在我的控制器中redirect_to:root,就像这样,

def destroy
  @movie = Movie.find params[:id]
  current_user.movies.destroy @movie
  redirect_to: root
end

我的控制台中出现(未找到404)错误。 rails服务器响应就是这个,

Started DELETE "/movies/49.json" for 127.0.0.1 at 2015-11-16 13:26:28 +0100
Processing by MoviesController#destroy as JSON
  Parameters: {"id"=>"49"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 5]]
  Movie Load (0.2ms)  SELECT  "movies".* FROM "movies" WHERE "movies"."id" = ? LIMIT 1  [["id", 49]]
   (0.1ms)  begin transaction
  HABTM_Movies Load (0.1ms)  SELECT "movies_users".* FROM "movies_users" WHERE "movies_users"."user_id" = ? AND "movies_users"."movie_id" = 49  [["user_id", 5]]
  SQL (0.2ms)  DELETE FROM "movies_users" WHERE "movies_users"."user_id" = ? AND "movies_users"."movie_id" = 49  [["user_id", 5]]
   (9.8ms)  commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 32ms (ActiveRecord: 10.6ms)


Started DELETE "/" for 127.0.0.1 at 2015-11-16 13:26:28 +0100

ActionController::RoutingError (No route matches [DELETE] "/"):

这就是routes.rb

Rails.application.routes.draw do

  devise_for :users, :controllers => { :omniauth_callbacks => "callbacks" }

  devise_scope :user do
    root to: 'application#angular' #Integrates Angular with Rails
    match '/sessions/user', to: 'devise/sessions#create', via: :post
  end

  resources :sessions, only: [:create, :destroy]
  resources :movies, only: [:create, :destroy, :index, :show]
  resources :movies_users, only: [:create, :destroy, :index, :show]

end

在控制台中修复错误响应的最佳方法是什么?

佣金路线的结果,

Prefix Verb     URI Pattern                            Controller#Action
new_user_session GET      /users/sign_in(.:format)               devise/sessions#new
user_session POST     /users/sign_in(.:format)               devise/sessions#create
destroy_user_session DELETE   /users/sign_out(.:format)              devise/sessions#destroy
user_omniauth_authorize GET|POST /users/auth/:provider(.:format)        callbacks#passthru {:provider=>/facebook/}
user_omniauth_callback GET|POST /users/auth/:action/callback(.:format) callbacks#:action
user_password POST     /users/password(.:format)              devise/passwords#create
new_user_password GET      /users/password/new(.:format)          devise/passwords#new
edit_user_password GET      /users/password/edit(.:format)         devise/passwords#edit
       PATCH    /users/password(.:format)              devise/passwords#update
       PUT      /users/password(.:format)              devise/passwords#update
cancel_user_registration GET      /users/cancel(.:format)                devise/registrations#cancel
user_registration POST     /users(.:format)                       devise/registrations#create
new_user_registration GET      /users/sign_up(.:format)               devise/registrations#new
edit_user_registration GET      /users/edit(.:format)                  devise/registrations#edit
       PATCH    /users(.:format)                       devise/registrations#update
       PUT      /users(.:format)                       devise/registrations#update
       DELETE   /users(.:format)                       devise/registrations#destroy
  root GET      /                                      application#angular
sessions_user POST     /sessions/user(.:format)               devise/sessions#create
sessions POST     /sessions(.:format)                    sessions#create
session DELETE   /sessions/:id(.:format)                sessions#destroy
movies GET      /movies(.:format)                      movies#index
       POST     /movies(.:format)                      movies#create
 movie GET      /movies/:id(.:format)                  movies#show
       DELETE   /movies/:id(.:format)                  movies#destroy
movies_users GET      /movies_users(.:format)                movies_users#index
       POST     /movies_users(.:format)                movies_users#create
movies_user GET      /movies_users/:id(.:format)            movies_users#show
       DELETE   /movies_users/:id(.:format)            movies_users#destroy

1 个答案:

答案 0 :(得分:0)

成功销毁电影记录后,您需要重定向到页面,通常是电影的索引页面。像这样:

def destroy
  @movie = Movie.find params[:id]
  if current_user.movies.destroy @movie
     flash[:success] = "Movie deleted!"
     redirect_to movies_path
  else
    flash[:error] = "Movie was not deleted!"
    redirect_to movies_path
  end
end

另一种解决方案(没有重定向):

  def destroy
    @movie = Movie.find params[:id]
    current_user.movies.destroy @movie
    respond_to do |format|
      format.json { head :no_content }
    end
  end