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:
@code
stands variable keeps code/information executed model method;@instance
stands model instance;path
stands path want redirect in case there no errors;render js: <...>
used instead ofredirect_to
because you've setrespond_to :html, :js
in controller, way redirect use javascript, otherwise you'll full rendered page redirecting ajax response.flash.now
allows access messages (in case, error messages) in same request, usingflash
allows in next request.
Comments
Post a Comment