1

I have ngResource directive included in my first project in order to be able to interact with RESTful API.

Then I setup my factory like this:

angular.module('app').factory('User', [
  '$resource', function($resource) {
    return $resource('/api/user_login/:id', { id: '@id' }, {
      update: { method: 'PUT' }
    });
  }
]);

In my controller I try to save User i.e to send POST request.

app.controller('loginCtrl', [
  '$scope', 'User', function($scope, User) {
    return User.save();
  }
]);

My second project's controller look like the following. I'm trying to reference create action.

class UserSessionsController < UserApplicationController
  respond_to :js, only: :create

  def create
    if @counterparty
      session[:counterparty_id] = @counterparty.id
      @counterparty.update(signed_in: true)
    else
      flash.now[:error] = 'Invalid email or password'
    end

    respond_with(@counterparty, layout: false)
  end
end

As a result I'm getting ActionController::UnknownFormat.

enter image description here

Started POST "/user_login" for 127.0.0.1 at 2016-02-18 16:04:58 +0200
Started POST "/user_login" for 127.0.0.1 at 2016-02-18 16:04:58 +0200
Processing by UserSessionsController#create as HTML
Processing by UserSessionsController#create as HTML
  Counterparty Load (0.3ms)  SELECT  `counterparties`.* FROM `counterparties`  WHERE `counterparties`.`email` IS NULL AND `counterparties`.`password` IS NULL LIMIT 1
  Counterparty Load (0.3ms)  SELECT  `counterparties`.* FROM `counterparties`  WHERE `counterparties`.`email` IS NULL AND `counterparties`.`password` IS NULL LIMIT 1
Completed 406 Not Acceptable in 1ms
Completed 406 Not Acceptable in 1ms

ActionController::UnknownFormat (ActionController::UnknownFormat):
  app/controllers/user_sessions_controller.rb:13:in `create'

How can I solve this?

2
  • Can you include full Rails log for that request? Commented Feb 18, 2016 at 14:03
  • @MichalSzyndel Updated the question Commented Feb 18, 2016 at 14:07

2 Answers 2

2

Change respond_to :js, only: :create to respond_to :json, only: :create

user factory is doing a request in json format, although the controller should respond to js (json included) that didn't work for op, so it's necessary to set respond_to json explicitly

Sign up to request clarification or add additional context in comments.

5 Comments

Can you explain how the answer fixes the trouble?
sure, user factory is doing a request in json format, although the controller should respond to js (json included) that didn't work for op, so it's necessary to set respond_to json explicitly
Great. Can you edit the answer to reflect the explaination?
What If I have to respond to both formats depending on request? I need js to exist in my project as well.
You can add multiple respond_to in your controller action, as Pitabas suggested
1

Replace the create function with following function

    def create
      respond_to do |format|
       if @counterparty
         session[:counterparty_id] = @counterparty.id
         @counterparty.update(signed_in: true)
         format.json  { render :json => @counterparty.to_json ,status: :ok}
       else
         format.json  { render :json => 'Invalid email or password',status: :unprocessable_entity}
       end
      end
   end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.