0
votes

I am appending code to a div block from a partial using unobtrusive javascript. Inside my views there are two folders called videos and posts. In the the video folder is a show.html.erb file. In this show.html.erb file there's a form to allow a user to create a post. The form attribute remote is set to true and I want the post that gets submitted by the form to be loaded onto the page (the same show.html.erb page) without having to refresh (AJAX functionality). When the form is submitted it is sent to the create method of the posts controller. In this method there's a

format.js

that calls views > posts > create.js.erb.
The code I have written for this file so far is:

$('#comments').append('<%=escape_javascript(render(:partial => 'posts'))%>');

comments is the div id for a block in the show.html.erb file. The partial 'posts' is inside the posts folder (views > posts > _posts). Right now this is not working properly and I believe its because the create.js.erb file is in a separate folder from the file whose div id its trying to reference. How do I get the javascript code from the posts folder to reference the div block inside the video show file? Thanks.

2

2 Answers

1
votes

I'm not sure if this is what you're asking but you can pass a partial the full path like this:

 $('#next_videos').empty().append('<%=escape_javascript(render(:partial => 'videos/partial_name'))%>');
0
votes

Being relatively new to rails and even newer to jquery I had an issue in identifying my problem. I thought that because my jquery code was in a separate folder from the div I wanted to reference that the code I had above wouldn't work properly. The answer (as I found out) is that as long as the div and the jquery are in the same DOM the reference works fine. I had a bug somewhere else in my code that was preventing the AJAX request from functioning normally. After fixing that my jquery code works as intended. Thanks