Search code examples
ruby-on-railsdevisehttp-status-code-406

Rails, Devise Invitations 406 Unacceptable error with STI


There are similar questions but I've gone over them and none of them have applied.

I am using Devise and Devise invitations. Rails 3.2.6.

The tricky thing is I have two user models. User and a single table inheritance setup where Artist inherits from User. I have two invitations controllers to allow sending invitations separately to both Users and Artists.

The issue right now is that artists are not able to accept their invitations. Upon form submit of updating their password, they get a 406 unacceptable error.

Relevant Code:

Routing:

  devise_for :users,
             :class_name => User,
             :controllers => { :invitations => 'user/invitations' }

  namespace :artist do
    # Special route for devise, artists invitation
    devise_scope :user do
      resource :invitation, :controller => :invitations, :only => [:create, :new, :update] do
        get :accept, :action => :edit
      end
    end
  end

Artist invitations controller

class Artist::InvitationsController < Devise::InvitationsController
  respond_to :html

  # PUT /resource/invitation
  def update
    self.resource = Artist.accept_invitation!(params[resource_name])

    if resource.errors.empty?
      resource.pending
      flash[:notice] = "Welcome, complete your artist agreement to get started"
      sign_in(:user, resource)
      respond_with resource, :location => after_accept_path_for(resource)
    else
      respond_with_navigational(resource){ render :edit }
    end
  end
end

Form description. edit.html.erb.

<%= form_for resource, :as => resource_name, :url => artist_invitation_path(resource_name), :html => { :method => :put } do |f| %>
  <%= f.hidden_field :invitation_token %>
  <%= f.label :password, class: 'control-label' %>
  <%= f.password_field :password, :placeholder => 'Enter a password' %>
  <%= f.label :password_confirmation, class: 'control-label' %>
  <%= f.password_field :password_confirmation, :placeholder => 'Confirm your password' %>
  <%= f.submit "Set my password", :'data-disable-with' => "Hang on..." %>
<% end %>

Accept headers

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:236
Content-Type:application/x-www-form-urlencoded

Returns

Request URL:http://localhost:3000/artist/invitation.user
Request Method:POST
Status Code:406 Not Acceptable

I can see that something in the format must be off because of the response adding in that .user to the end. I can not for the life of me see why or where that is happening. Please let me know if there's any info I left out which could help.


Solution

  • Got it, finally.

    The key is to look precisely at what resource_name is giving you throughout the form and controller actions. In my case it continued to fallback to :user when it needed to be :artist and as Frost mentioned in a comment, the generated form html changes greatly if you pay attention to where resource_name is used. The update action in the controller needed to be overridden to use :artist directly and the form_for needed to be tweaked to remove resource_name from arguments and the :as argument.