devise, User#Update with remote => True, how to pass back errors?
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?
精彩评论