Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to update DOM after modal form submission on Rails
    primarykey
    data
    text
    <p>I am trying to handle my sign up process with modal forms, using Twitter Bootstrap. I can get the form to submit, the modal to close, and everything works fine, except the DOM is not unobtrusively updated.</p> <p>After a user signs in, the navigation bar (which is rendered with a partial and has ruby in it) should no longer have a link for "Sign up", and it should acquire a link "Sign out".</p> <p>Since I can't use <code>render</code> in my coffeescript file which manages the form submission, how can I re-render the nav bar so to speak to make it reflect the successful sign up/modal form submission?</p> <p><strong>Modal Form (HAML):</strong></p> <pre><code>= simple_form_for @user, remote: true, html: {data: {type: 'html'}, class: 'form-horizontal'} do |f| %div.modal-header %button.close{"aria-hidden" =&gt; "true", "data-dismiss" =&gt; "modal", :type =&gt; "button"} × - if @user.errors.any? #error_explanation %h2= "#{pluralize(@user.errors.count, "error")} prohibited this user from being saved:" %ul - @user.errors.full_messages.each do |msg| %li= msg %div.modal-body =render 'form', f: f %div.modal-footer = f.button :submit, name: 'Sign Up', class: "btn-primary", id: "new_user_button" =link_to "Sign In", signin_path </code></pre> <p><strong>jquery handling form submission:</strong></p> <pre><code>$ -&gt; $("#new_user_button").click -&gt; valuesToSubmit = $("#new_user").serialize() $("#signup_modal").modal "hide" $.ajax( url: "/users" data: valuesToSubmit type: "POST" ).success (data, status) -&gt; #update the DOM? </code></pre> <p><strong>Controller:</strong></p> <pre><code>def create @user = User.new(params[:user]) respond_to do |format| if @user.save #SignupMailer.new_subscriber(@user).deliver sign_in @user format.html { redirect_to root_path, notice: "Thank you for signing up!" } format.json { render json: @user, status: :created, location: @user } # format.js {'create'} else format.html { render action: "new" } format.json { render json: @user.errors, status: :unprocessable_entity } #format.js end end </code></pre> <p><strong>Navigation Partial (HAML):</strong> need to unobtrusively change these elements after form submission and modal close!</p> <pre><code>%ul.nav.nav-pills.pull-right -if signed_in? %li{:class =&gt; "#{'active' if current_page?(user_path(current_user))}"}=link_to "Your page", user_path(current_user) %li=link_to "Sign out", signout_path, :class =&gt; "#{'active' if current_page?(root_path)}" -else %li= link_to_modal 'Sign Up', new_user_path, :width=&gt; "600", :height =&gt;"400", :remote =&gt; true, :id =&gt; "new_user_link" %li=link_to "Sign In", signin_path, :class =&gt; "#{'active' if current_page?(root_path)}" </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload