0

My respond_to block does not redirect back to the '/dashboard' view (url stays at 'gcal_user.19') and results in a "406 Not Acceptable" error when I click on the delete link in the dashboard view. Tried debugging, googling, stack overflow but all efforts been fruitless.

Using Rails 3.2.13, Ruby 1.9.3
The app includes jquery and jquery_ujs (unobtrusive js) in application.js

Dashboard View (Haml):

%div.control-group.controls
  = link_to "Delete Gcal User", @gcal_user, method: :delete

GcalUser Controller:

  def destroy
    @gcal_user = current_user.gcal_user

    # -- commented out for debugging --
    # if @gcal_user.delete
    #   flash[:notice] = "#{@gcal_user.username} deleted"
    # end

    respond_to do |format|
      format.html { redirect_to user_root_path }
    end
  end

config/routes.rb

  get "home/index"
  root :to => 'home#index' #, as: '/'
  devise_for :users
  resource :gcal_user
  match "dashboard" => 'home#dashboard', as: :user_root

Route:

DELETE /gcal_user(.:format)              gcal_users#destroy

Clicking on the Delete link correctly reaches the destroy method. The issue occurs with the respond_to block.

Other code samples seem to have this working... I can't figure out what I'm missing. Any ideas?

Second, the 406 error is due to a request type mismatch, I believe, how do I check the type of the request and response being generated? If there is a mismatch, where in the code would I be able to change the request type?

4

3 回答 3

2

"406" error represents for lack of right format to respond.

You do not have right format to respond because your controller action results an error, and the error has no format.

Why error? You delete method failed.

Why failed? You don't have an id assigned to the obj.

Why no id? You have not defined it in your routes.

See your routes. It's wrong

DELETE /gcal_user(.:format)              gcal_users#destroy

There is no id. The right things should be

DELETE /gcal_user/:id(.:format)              gcal_users#destroy

Try check and redefine your routes.

于 2013-08-19T05:41:03.620 回答
2

As you mentioned in Billy Chan's answer, the url is "/gcal_user.19", so that means the controller is trying to respond to the format with type '19'. Try redefining your routes as resources :gcal_user, and you can pass only: :destroy if you wish to remove unneeded routes. I would have replied as a comment, but I don't have the required reputation to do so yet.

于 2013-08-19T06:31:36.813 回答
0

The URL is not called with DELETE method and hence the destroy is not called. What is called is the Users#destroy with GET method and it is giving 406 error. The possible causes and their solution is given by Intrepidd above.

Try something like this:-

devise_scope :user do root :to => 'home#dashboard' end

于 2013-08-19T05:45:01.697 回答