vendredi 19 février 2016

Respond_to format.js won't work

I can't get respond_to to work with format.js.

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

    respond_to do |format|
      format.js
      format.json { render json: @counterparty }
    end
  end
end

views/user_sessions/create.js.erb

alert("Please, notice me!")

server logs

Started POST "/vendor_login" for 127.0.0.1 at 2016-02-19 12:01:43 +0200
Processing by VendorSessionsController#create as JS
  Parameters: {"utf8"=>"✓", "email"=>"ror@example.com", "password"=>"[FILTERED]", "commit"=>"Sing in"}
  Counterparty Load (0.3ms)  SELECT  `counterparties`.* FROM `counterparties`  WHERE `counterparties`.`email` = 'ror@example.com' AND `counterparties`.`password` = 'faa90d7e56eb908c' LIMIT 1
   (0.1ms)  BEGIN
   (0.1ms)  COMMIT
  Rendered vendor_sessions/create.js.erb within layouts/vendor_application (0.0ms)
  Rendered shared/_flash.slim (0.1ms)
Completed 200 OK in 634ms (Views: 631.6ms | ActiveRecord: 0.5ms)

What's up with this?

Now, the most interesting part about it!

If I go like this my alert shows up, but that option doesn't suit me.

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

But that option doesn't fit me.

What do I do?

Aucun commentaire:

Enregistrer un commentaire