Gerome John Pangan Gerome John Pangan - 3 months ago 9
Ajax Question

rendering a partial with object in js.erb file

this is my index/post for rendering a comment

<% @posts = Post.where(user_id: current_user.all_user_ids).order("created_at desc") %>

<%@posts.each do |post| %>
<div id="comment-action">
<button id="<%= "#{post.id}" %>"> View comment</button>
<%= link_to "View Comments", "#post_comments_#{post.id}" %>

<section class="comments-section" id="post_comments_<%= post.id %>" >
<br>
<div id="comments">
<%= render 'commenters/newsfeedcomment', obj: post %>
</div>

<% if current_user == post.user %>
<h6 align="right"><%= link_to 'Edit', edit_post_path(post) %></h6>
<h6 align="right"><%= link_to 'Delete', { :id => post ,:controller => 'posts',:action => 'destroy'} %></h6>
<% end %>
</section>
</div>
<% end %>


now i past a object to my _newsfeedcomment which is obj: post

heres my newsfeedcomment

<% obj.commenters.each do |c| %>
<li class = 'comments'>
<br><%= image_tag c.user.profile.avatar.url, size:"50x50" %>
<a href="<%= "#{profile_path(c.user.profile)}" %>">
<%= c.user.profile.firstname + " " + c.user.profile.lastname %></a> : <%= c.comment %>
<br><%= time_ago_in_words(c.created_at) %> ago
</li>

<% if current_user == c.user %>
<%= link_to "Delete comment", [c.post, c], method: :delete %>
<%= link_to "Edit comment", edit_post_commenter_path(c.post, c) %>
<% end %>
<% end %>


and i want to render my newsfeedcomment again for my ajax

$('#comments').append("<%= j render(:partial => 'commenters/nesfeedcomment', obj: post) %>");


and this is my error


undefined local variable or method `post' for #<#:0x007f8660e313e8>


i guess rails cant read my object? is there any approach so i can finish my project please someone help me...

heres my controller

def create
@comment = @post.commenters.create(params[:commenter].permit(:comment))
@comment.user_id = current_user.id
@comment.save

if @comment.save
respond_to do |format|
format.html {redirect_to posts_path}
format.js
end
else
redirect_to posts_path
end


end

Answer

i create another render file just like newsfeedcomment

commenters/commentlist.html.erb

    <li class = 'comments'>
<br><%= image_tag @comment.user.profile.avatar.url, size:"50x50" %> 
<a href="<%= "#{profile_path(@comment.user.profile)}" %>">
<%= @comment.user.profile.firstname + " " + @comment.user.profile.lastname %></a> : <%= @comment.comment %>

<br><%= time_ago_in_words(@comment.created_at) %> ago

<% if current_user == @comment.user %>
        <%= link_to "Delete comment", [@comment.post, @comment], method: :delete %>
        <%= link_to "Edit comment", edit_post_commenter_path(@comment.post, @comment) %>
    <% end %>
</li>

and this is my create.js.erb

var content = "<%= j render(:partial => 'commenters/commentlist', obj: @comment) %>";

var $post = $("#post_comments_<%= @comment.post_id %>");
var $last_comment = $post.find('#comments > li:last');

if ($last_comment.length > 0) {
$last_comment.after(content);
} else { 
var $comments = $post.find('#comments');
$comments.prepend(content)          
}