sjain
sjain

Reputation: 23344

Rendering a partial using ajax

It seems simple but my partial is not rendered in a view(dash.html.erb) using ajax.

routes.rb

match "users/dash" => "users#dash"

users controller

def dash
     respond_to do |format|
    format.js
  end
   end

dash.html.erb

<div id= "mydiv">This view is not shown using ajax.</div>

dash.js.erb

$('#mydiv').html('<%= raw escape_javascript render("cart_payment") %>');

My partial- _cart_payment.html.erb

<p>This is a partial.</p>

UPDATE:

users/index.html.erb

<script type= "text/javascript">

$.ajax({
  url: "users/dash",
  data: {  
  //params if needed

  }
});

</script>

Please note that I am calling- users url in browser because ajax is called from here. Is it not correct way or Am I missing something?

Upvotes: 0

Views: 422

Answers (1)

Dylan Markow
Dylan Markow

Reputation: 124419

When you call the dash method remotely, it actually is executing the $('#mydiv').html... code. The problem is, your index.html.erb file has no <div id='mydiv'> to update in the first place.

If you replace your index.html.erb with this, it should work:

<div id='mydiv'></div>

<script type= "text/javascript">
  $.ajax({
    url: "users/dash",
    data: {  
    //params if needed
    }
  });
</script>

Upvotes: 1

Related Questions