title: Making Devise respond to both html and json?
body: <p>I am working on an app which needs has both website and an api and I am implementing a common Devise authentication for that. To implement this requirement I am overriding Devise Registrations Controller to respond to both html and json. For json I want the controller to return the user data and I am able to implement this, but for html I want to use the original devise behavior of signing in and redirecting to root path. </p>
  
  <p>The code for Registrations Controller is:</p>
  
  <pre><code>class RegistrationsController &lt; Devise::RegistrationsController
  
   def create
    @user = User.create(user_params)
    respond_to do |format|
      format.html {
        @user.save ? (super) : (render :new)
      }
      format.json {
        @user.save ? (render :json =&gt; {:state =&gt; {:code =&gt; 0}, :data =&gt; @user }) : 
                     (render :json =&gt; {:state =&gt; {:code =&gt; 1, :messages =&gt; @user.errors.full_messages} })
      }
    end
  end
  
  private
  
    def user_params
       params.require(:user).permit(:email, :password)
    end
  end 
  </code></pre>
  
  <p>Using this controller I get a validation <strong>error Email has already been taken</strong> but when I check my logs it shows the user has been created.</p>
  
  <p>Can anyone tell me what error I am doing? I just want my controller to fallback to original devise functionality in case of html request.</p>