I'm trying to get a new comment form to load under a post. I have multiple posts on the page so I dynamically create the id's for each post using its primary id. I then pass that id to my script and I can see in the browser that both the html and the script are being rendered with the correct post id's but for some reason my new comment partial is not being rendered.
I'm using jQuery Mobile so I tried targeting the page-content div and it gets rendered fine but it won't render at all when I just try targeting the div that contains my post. Its as if the id doesn't exist but I can see it when I view source. Is there an issue with targeting dynamically created id's or is there some kind of trick to it?
Here is my javascript:
$("#post_<%= escape_javascript(@post.id.to_s) %>").append("<%= escape_javascript(render "comments/new", item: :post) %>");
My action:
def load_new_comment_partial
@comment = Comment.new
@post = Post.find(params[:id])
respond_to do |format|
format.js { render "comments/new" }
end
end
This js code is tricky sometimes, you can try this:
$('<%= "#post_#{@post.id.to_s}" %>').append('<%= escape_javascript(render "comments/new", item: @post) %>');
That code above should be on load_new_comment_partial.js.erb
I've changed the quotation and also the item param I've changed to @post instead of :post
And in your action you can replace
format.js { render "comments/new" }
with
format.js
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.