Deepender Singla
Deepender Singla

Reputation: 997

Rails render not refrshing the page

I am working on ROR app . The app view contain ajax and jquery code :

jQuery.ajax({
  data:     'val=' + val,
  dataType: 'script',
  type:     'post',
  url:      "/portfolio/update"
});

The update action contain this code:

def update
  j = params[:val]

  # Buisness logic code
  redirect_to root_url, notice: "update done"
end

current View and Root url is same - Portfolio/show Now the button on view (which is root_url only)is doing the business logic fine , but the page is not getting refreshed , Whereas other simple form button on views are doing it. After pressing button i am getting this in rails server:

Rendered portfolio/show.erb (1.5ms)
Completed 200 OK in 24ms (Views: 4.1ms)

Any guesses, but the page is not refreshing by its own.

Upvotes: 0

Views: 2140

Answers (2)

maximus ツ
maximus ツ

Reputation: 8065

Ajax is used to provide functionality where page refresh is not desired. If you want page refresh, then you should rather use html form and submit button.

  <%= form_for %>
   .....
  <% submit %>

or with Ajax, you need to do it manually on success call back,

like this,

$.ajax({

 success: function(){
  window.location.reload();
 }
);

Upvotes: 1

Billy Chan
Billy Chan

Reputation: 24815

No guess needed. Your controller responded with wrong type.

redirect_to can only work under html response. When your request type is 'script' which is 'js', nothing can be rendered.

To correct it

def update
  # blah blah
  respond_to |format|
    format.html { redirect_to some_path }
    format.js # This will render default js template views/portfolio/update.js.erb
  end
end

Then prepare the js template

$('#div_to_update').html('new html code to update the view')

Upvotes: 1

Related Questions