1

I have almost done! but I have an issue, in my Controller file have this:

def show
   @user = User.find(params[:id])
   @posts = @user.posts.paginate(page: params[:page])
end

Then I have this piece of code in my file show.html.erb:

<div class="span8">
   <%= render 'follow_form' if signed_in? %>
 <% if @user.posts.any? %>
    <h3>Microposts (<%= @user.posts.count %>)</h3>
    <div id='posts'>
      <div class='page'>
     <ol class="microposts">
        <%= render @posts %>
     </ol>
  </div>
</div>
   <% end %>
 </div>

At the bottom of this file, I have a Javascript code that I have taken from the tutorial: https://github.com/amatsuda/kaminari/wiki/How-To:-Create-Infinite-Scrolling-with-jQuery

In the same folder I have the file index.js.erb with:

$("#articles").append("<div class='page'><%= escape_javascript(render(@users)) %></div>");
$("#posts").append("<div class='page'><%= escape_javascript(render(@posts)) %></div>");

In a partial _posts.html.erb have this:

<div class='article'>
  <%= image_tag(user.picture_url, :width => 50) %>
  <%= link_to user.name, user %>
  <% if current_user.admin? && !current_user?(user) %>
    | <%= link_to "delete", user, method: :delete,
                              data: { confirm: "You sure?" } %>
  <% end %>
</div>

The first one already works in my file index.html.erb, the problem is with the second piece of code, when I try to render the partial at @post, It brings the follow log:

**
'nil' is not an ActiveModel-compatible object that returns a valid partial path.
Extracted source (around line #2):
1: $("#articles").append("<div class='page'><%= escape_javascript(render(@users)) %></div>");
2: $("#posts").append("<div class='page'><%= escape_javascript(render(@posts)) %></div>");
**

How can I render that partial?

Thanks a lot :D

9
  • 1
    Is the line I need to render to the "_feed_item.html.erb" inside "_feed.html.erb" means you are already in the _feed.html.erb page and just want to load more _feed_item.html.erb when user scrolls down to the bottom? Commented Sep 14, 2012 at 5:38
  • Yes, you got it!, how can i do that? Commented Sep 17, 2012 at 1:44
  • are you using rails 2.x.x or 3.x.x ? Commented Sep 17, 2012 at 3:16
  • Also let us know some more details like which js library u r using etc. I will try to make a dummy app for you. Commented Sep 17, 2012 at 3:23
  • by the way I'm using rails 3.2.8 Commented Sep 17, 2012 at 5:34

2 Answers 2

1

I usually use the .js.erb template to render the partial into a var then set it on the page using JS. Be sure to escape_javascript the template content otherwise you will get JS errors.

<% template_html = render :partial => '_feed_item.html.erb' %>
<%="$('div#content').html('#{escape_javascript(template_html)}</div>');" %>

I think the above should work in erb. I use HAML so mine looks like:

-template_html = render :partial => 'partial_template'
!="$('div#content').html('#{escape_javascript(template_html)');"

Cheers.

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

Comments

0

Based on your In script I'm calling to /users?page=, and I was wondering that It may calls the line, you must make sure that your controller is populating the @posts variable if your view is using this. Can you show the controller method users?

5 Comments

users?page script is inside the show.html.erb, so in the controller I have this: def show @user = User.find(params[:id]) end
You have a def users ... .. end method right? Would you please add that in your post? This method must populate @posts also.
Ups, i don't have defined that method, but it works with the show method @user :s
I've done! I must replace one render, because of the abstract function, but now it works! thanks!
It would be nice if you share your final code here. That might help others. Well I dont know i really deserve the accept or not :)

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.