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 of redirect_to because you've set respond_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, using flash allows in next request.

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -