1

How can I call a partial from an Ajax function, I need to render the content of a partial inside my home.html.erb, here the code:

<div class="span8">
  <h3>Micropost Feed</h3>
    <div class="posts">
        <%= render 'feed' %>
    </div>
</div>

At bottom I have a script, the problem code are in these lines:

if(nearBottomOfPage()) {
  loading=true;
  page++;
  $.ajax({ // send an ajax-request to load the next page
    url: '/feed?page=' + page,
    type: 'get',
    dataType: 'script',
    success: function() { // after successfully completing the ajax-request redraw the    sausage-navigation.
      $(window).sausage('draw');
      loading=false;
    }
  });

The "url" parameter is where I don't know what to write to call a partial, this partial is call _feed.html.erb and It's inside the same folder of Home.html.erb, finaly in my controller action I have this:

def home
  if signed_in?
    @post  = current_user.posts.build
    @feed_items = current_user.feed.order(:created_at).page(params[:page])
  end  
  respond_to do |format|
      format.js  { render :partial => 'feed'}
      format.html # index.html.erb
      format.xml  { render :xml => @feed_items }
    end
 end

How can I call that partial from the Ajax function?

Thanks a lot.

1 Answer 1

2

You cant call the partial directly via a URL. Only pages/views in the /public dir can be accessed directly.

You need to create a controller action that renders the 'feed' partial, and a corresponding route. Then use the path for that route as your AJAX URL.

Sign up to request clarification or add additional context in comments.

1 Comment

Then I have the content that I need at feed.html.erb, how can I render it by escape_javascript(????) :s

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.