jquery - rails ajax call for an action renders view twice -
i have user registration form in rails app. didn't use standard rails form, custom html form. data passed "create" action in userscontroller ajax call. html.erb:
<!-- register form --> <div class="user_register"> <form> <label><%= t(:full_name)%></label> <input type="text" id="name"/> <br /> <label><%= t(:email_address)%></label> <input type="email" id="regmail"/> <br /> <label><%= t(:password)%></label> <input type="password" id="regpassword"/> <br /> <label><%= t(:password_again)%></label> <input type="password" id="password_confirm"/> <br /> <!--div class="checkbox"> <input id="send_updates" type="checkbox" /> <label for="send_updates">send me occasional email updates</label> </div--> <div class="action_btns"> <div class="one_half"><a href="#" class="btn back_btn"><i class="fa fa-angle-double-left"></i> <%= t(:back)%></a></div> <div class="one_half last"><a href="#" id="register_button" register_url=<%= root_url+'/create_user' %> class="btn btn_red"><%= t(:sign_up)%></a></div> </div> </form> </div> <script type="text/javascript"> $( "#register_button" ).click(function() { var email = $('#regmail').val(); var name = $('#name').val(); var register_url = $('#register_button').attr('register_url'); var password = $('#regpassword').val(); var password_confirm = $('#password_confirm').val(); jquery.ajax({ url: register_url, type: 'post', data: { 'user' : { 'password': password, 'password_confirmation': password_confirm, 'name': name, 'mail': email} }, datatype: 'html', success: function() { window.location.reload(true); }, beforesend: function(xhr) { xhr.setrequestheader('x-csrf-token', $('meta[name="csrf-token"]').attr('content'));}, }); }); </script> and code controller is:
def create if params[:user][:password].eql?(params[:user][:password_confirmation]) @user = user.new({:name=>params[:user][:name], :mail=>params[:user][:mail], :username=>params[:user][:username], :hashed_password=>digest::sha1.hexdigest(params[:user][:password])}) #password = params[:user][:password] #mail = params[:user][:mail] respond_to |format| if @user.save format.html { redirect_to root_url, notice: t(:congratulations_can_now_log_in) } #format.html { redirect_to "/create_session", :mail => mail, :password => password, notice: 'user created.' } format.json { render json: @user, status: :created, location: @user } else format.html { redirect_to root_url, notice: t(:error_while_registering) } format.json { render json: @user.errors, status: :unprocessable_entity } end end else redirect_to :back, notice: t(:error_while_registering) end end
ajax calls "create" action creating new user, , action renders view. if ajax succeed, page loaded again(success: function() { window.location.reload(true); }). 2 calls same url, first action , second ajax on success. problem if first contains notice or alert these can't viewed, second get, delete them (as reload).
what avoid second call (reloading page ajax), when insert empty function or function nothing in success option, ajax call failed.
what do?
Comments
Post a Comment