jquery - Rails - render JavaScript with AJAX or redirect -
i have rails remote form_for. several reasons way better idea use ajax submit form's contents. if there errors saving form want able render .js.erb file page updates error div partial displays errors.
if there no errors want able redirect , display flash message.
is possible?
you can try in controller method:
if @code flash[:success] = 'success message' render js: "window.location = '#{path}'" else flash.now[:alert] = @instance.errors.full_messages render partial: 'your/partial' end detailed explanation:
@codestands variable keeps code/information executed model method;@instancestands model instance;pathstands path want redirect in case there no errors;render js: <...>used instead ofredirect_tobecause you've setrespond_to :html, :jsin controller, way redirect use javascript, otherwise you'll full rendered page redirecting ajax response.flash.nowallows access messages (in case, error messages) in same request, usingflashallows in next request.
Comments
Post a Comment