ruby on rails - redirect_to not working with ajax -


i have created ajax form in bootstrap modal creates new post database. form is:

<%= form_for @post, remote:true, :html => {:id => "share-needs-form"} |f| %>     <div class="modal-body">         <div id="shareneedsmodalfirststep">         <div class="row" style="margin-bottom: 10px">             <ul class="error-alert hidden share-needs-form-errors col-lg-10 col-centered" id="share-needs-error-alerts"></ul>     </div>     <div class="row"style="margin-bottom: 10px;">         <div class="col-lg-10 col-centered">             <%= f.text_field(:title, :class => "form-control", :placeholder => "add title e.g need designer, in return can code you", :id => "title") %>         </div>     </div>     <div class="row" style="margin-bottom: 10px;">         <div class="col-lg-10 col-centered">             <%= f.text_area(:description, :rows => "5", :class => "form-control", :id => "description", :placeholder => "write description idea, people can offer exchange know want accomplish") %>         </div>     </div>     <div class="row">         <div class="col-lg-10 col-centered">             <%= f.text_field(:keywords, :class => "form-control", :id => "keywords", :placeholder => "add keywords (not more 5 , comma(,) separated)") %>         </div>     </div>     </div>     <div class="row hidden" id="shareneedsmodalsecondstep" >         <div class="well" style="max-height: 250px; overflow-y: scroll;">             <div>             <a href="#" style="font-size: 17px;">i need designer, in return can code you</a>             <p>i need designer game project, working on small 2d game, can provide coding skills you. @ making web applications, and...</p>             </div>             <hr />             <a href="#" style="font-size: 17px;">i need designer, in return can code you</a>             <p>i need designer game project, working on small 2d game, can provide coding skills you. @ making web applications, and...</p>             <hr />             <a href="#" style="font-size: 17px;">i need designer, in return can code you</a>             <p>i need designer game project, working on small 2d game, can provide coding skills you. @ making web applications, and...</p>         </div>     </div>     </div>     <div class="modal-footer">     <button type="button" class="btn btn-default" data-dismiss="modal">close</button>     <button type="button" class="btn btn-warning hidden" id="shareneedsmodalbackbutton">back</button>     <button type="button" class="btn btn-warning" id="shareneedsmodalcontinuebutton">continue</button>     <%= submit_tag "share", :class => "btn hidden btn-warning", :id => "shareneedsmodalformsubmitbutton" %>     </div> <% end %> 

and here corresponding controller:

def create     @post = post.new(post_params)     if @post.save         redirect_to post_path(@post.id)     else         @errors = @post.errors.full_messages         render :json => @errors.to_json     end end 

as can see, create method creates new post form fields received post_params. else block works fine means errors returned , displayed in view using following javascript:

$("#share-needs-form").bind("ajax:complete", function(evt, data, status, xhr) {     console.log(data.responsetext);     if(typeof data == "object") { //errors returned json "object"         $("#shareneedsmodalbackbutton").click();         var form_errors_holder = $("#share-needs-error-alerts");         form_errors_holder.removeclass("hidden");         form_errors_holder.html("");         var errors = data;         for(var = 0; < errors.length; i++) {             form_errors_holder.append("<li>" + errors[i] + "</li>");         }     } }); 

but in controller when post saved not redirect post_path instead returns whole html of post_path page. why redirect not working , how fix this? please help.

actually, sending js request. therefore, server sent response in js format. this, have write format of response in create method, following.

def create   @post = post.new(post_params)   respond_to |format|     if @post.save       format.html { redirect_to @post }       format.js     else       format.html { render :new }       format.js     end   end end 

and, make file app/views/posts/create.js.erb , write following code.

<% if @post.errors.empty? %>    window.location = "<%= post_path(@post) %>"; <% end %> 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -