Monday, 26 August 2013

Ruby on Rails Controller - html submit but allow for error/validation js

Ruby on Rails Controller - html submit but allow for error/validation js

Background: I'm using Devise with a modal window to let users sign up/in
with either Facebook, Google or an email/password. The modal is working
great for Facebook and Google and email signups. I'm not using ajax for
the submit:
<%= form_for(resource, :as => resource_name,
:url => registration_path(resource_name),
:html => {:id => "sign_up_user"},
) do |f| %>
I overwrote Devise's registration controller to use mine. I have this in
my Registration controller:
registrations_controller.rb
respond_to :html, :json, :js
def create
build_resource(sign_up_params)
if resource.save
resource.update_column(:last_provider_login, 'email')
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_navigational_format?
sign_up(resource_name, resource)
return render :json => {:success => true}
else
set_flash_message :notice,
:"signed_up_but_#{resource.inactive_message}" if
is_navigational_format?
expire_session_data_after_sign_in!
respond_with resource, :location =>
after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
@poss_user = User.find_by_email(params[:user][:email])
respond_to do |format|
format.js { render 'devise/registrations/create_error' }
end
end
end
The submit works fine when there are no errors and I'd like to keep the
hmtl submit for this (no :remote=>true in the view that calls this);
however, when I try to add error handling to the modal, I find that it
only works with Ajax.
What I'd like is to submit via html and keep my registration code working
as-is if save is successfult but if there are errors, then have the
controller run my 'create_error.js.erb' file.

No comments:

Post a Comment