开发者

devise, User#Update with remote => True, how to pass back errors?

开发者 https://www.devze.com 2023-03-21 09:46 出处:网络
I have rails 3 + devise. And I have the user#edit form using remote true. Problem is I can\'t figure out how to handle errors. It\'s easy enough in the /registrations/update.js.erb to write JS to ha

I have rails 3 + devise. And I have the user#edit form using remote true.

Problem is I can't figure out how to handle errors. It's easy enough in the /registrations/update.js.erb to write JS to handle the success case, but how do I handle errors. I image I don't want that being handled in the JS file as a condition, right?

It might be nice to have something like:

respond_to do |format|
  if @user.errors.blank? == true && @user.save
    format.js
  else
    format.js { render :errors, :notice => @user.errors}
  end
end

But I 开发者_如何学运维don't see how to make that work with devise?

Any suggestions? Seems like a lot of devise users must have added remote-true to their user edit forms?

Thanks


It seems to me what you have should work. You have it all, you just did not show your .js partials which I'm sure you have':

Update

So for your routes you have:

app/config/routes.rb

devise_for :users, :controllers => {:registrations => "registrations"}

And for you controller you have:

app/controllers/registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController
  def new
    super
  end

  def create
    super
    #or something similar to update.js
  end

  def update
  respond_to do |format|
    if @user.save
      format.js {render 'updated.js'} #or whatever the name of our file is 
    else
      @errors = @user.errors
      format.js { render 'errors.js'}
    end
  end
  end
end

And that doesn't work?

0

精彩评论

暂无评论...
验证码 换一张
取 消