0

I'm trying to use javascript with AJAX in Rails, but is not working.

The login.hmtl.erb file:

<%= form_for :web_app, :html => { :id => 'login_form' } , :url => {:action => 'authentication'}, :remote => true  do |f| %>

The web_app_controller.rb action:

  def authentication
    respond_to do |format|
      format.js
    end
  end

The authentication.js.erb file

console.log("hi");

I get this error:

ActionController::UnknownFormat

I have done this before and worked fine, but don't know why I can't make it work. I do not know what I'm missing.

Update

log:

Started GET "/login" for 127.0.0.1 at 2017-11-21 03:05:14 -0300 Processing by WebAppController#login as HTML Rendering web_app/login.html.erb within layouts/application Rendered web_app/login.html.erb within layouts/application (15.1ms) Completed 200 OK in 77ms (Views: 73.2ms | ActiveRecord: 0.0ms)

Started POST "/login" for 127.0.0.1 at 2017-11-21 03:05:43 -0300 Processing by WebAppController#authentication as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"sFdGP449Z0Av5eMUPFYOAB/zvTAFViOoDcjFIfXWwu8/oj0iCpnN6fznk6f5AhQU7LSub+cvW2MWtMTD7SwgWA==", "web_app"=>{"code"=>"AQBIzFfceuIFFFsepBU2qzZfbY4nefzDLJFHQk8ZQ0rfp8mdBQHW4PLtWA_558Iro6HJiAKmN7_WI4aioLB96g42K-LPEHjbHJXnKUT_-KOxTHPsgfWXjx0m6yvuIF64Fd3KQTIgvX7AU3y4ibgnQVJFme6omcNl6sBCbxerDav6T3t6dEGYin8f8I0ewbAEvcLpu2u08ZtNH6RbPEo93rZVmAoc9Zu4TsDTSQ_R0-CQah72ZBaIr9bU3MesjuiXqRNj8dxWIrUnUlfE0rCo5gBK", "csrf"=>"abcd"}} Completed 406 Not Acceptable in 3ms (ActiveRecord: 0.0ms)

ActionController::UnknownFormat (ActionController::UnknownFormat):

exsnake
  • 1,767
  • 2
  • 23
  • 44

1 Answers1

0

In your web_app_controller.rb

Add this line after controller class declaration.

respond_to :js, only: [:authentication]

Remove respond_to block from authentication method. Then resubmit request. Check browser console to see log.

SSR
  • 6,398
  • 4
  • 34
  • 50