I am trying to create a rails login process using an application that will allow the user to subscribe / unsubscribe through a mobile application.
I created a SessionController like this:
class SessionsController < Devise::SessionsController def create resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new") set_flash_message(:notice, :signed_in) if is_navigational_format? sign_in(resource_name, resource) respond_to do |format| format.html { super } format.json { render :status => 200, :json => { :error => "Success", :user => resource}.to_json } end end def destroy super end end
My routes:
devise_for :users, :controllers => {:sessions => "sessions"} resources :users
Then I have the following specification to test the process:
require 'spec_helper' describe SessionsController do describe "POST 'signin'" do before (:each) do @user = Factory(:user) end it "should login with json request" do @expected = @user.to_json post :create, :user => {:email => ' user@test.com ', :password => 'please'}, :content_type => 'application/json', :format => :json response.body.should == @expected end end end
And I get the following error:
Failure/Error: post :create, :user => {:email => ' user@test.com ', :password => 'please'}, :content_type => 'application/json', :format => :json AbstractController::ActionNotFound: Could not find devise mapping for path "/users/sign_in.json?content_type=application%2Fjson&user%5Bemail%5D=user%40test.com&user%5Bpassword%5D=please". Maybe you forgot to wrap your route inside the scope block?
[EDIT] The functionality seems to be in order, but only the test is broken; because if I ran this little script:
require 'rest_client' require "active_support/core_ext" RestClient.post "http://localhost:3000/users/sign_in", {:user => {:email => ' user@test.com ', :password => 'please'}}.to_json, :content_type => :json, :accept => :json
I get the following result:
"{\"error\":\"Success\",\"user\":{\"email\":\" user@test.com \",\"name\":\"First User\"}}"
which is expected.