Note that there are some explanatory texts on larger screens.

plurals
  1. POMessage stream using JQuery PeriodicUpdater and Rails
    primarykey
    data
    text
    <p>I'm trying to create an AJAX message stream in my Rails 3 app using jQuery. The app shows posts and replies to the post. Right now it requires the user to refresh the browser to update:</p> <pre><code>//in app/views/posts/_replies.html.erb &lt;div class="replies"&gt; &lt;% for reply in @replies %&gt; &lt;div class="reply"&gt; &lt;p&gt;&lt;%= reply.body %&gt;&lt;/p&gt; &lt;/div&gt; &lt;% end %&gt; &lt;/div&gt; </code></pre> <p>I'm trying to use the jQuery port of PeriodicalUpdater (https://github.com/RobertFischer/JQuery-PeriodicalUpdater/) to ajaxify the replies as a constantly-updating stream.</p> <p>Based on Ryan Bates' Railscast (http://railscasts.com/episodes/136-jquery), here is what I've got:</p> <pre><code>// in public/javascripts/application.js $(document).ready(function(){ $.PeriodicalUpdater('/replies', { method: 'post', data: ???, minTimeout: 1000, maxTimeout: 8000, multiplier: 2, type: 'json', maxCalls: 0, autoStop: 0 }, function(data) { ???? }); }); </code></pre> <p>I'm new to js and jQuery, so it's not really clear to me how to pass the current post to the server nor how to have it automatically render the <code>replies</code> partial. Here is the rest of my code:</p> <pre><code>// in app/views/posts/show.js.erb $("#replies").append("&lt;%= escape_javascript(render 'replies') %&gt;"); // app/controllers/posts_controller.rb class PostsController &lt; ApplicationController def replies @replies = Post.find(params[:id]).replies respond_to do |format| format.js end end end </code></pre> <p>Is there some way to pass the ID of the current <code>post</code> to the controller? Maybe even pass the ID of the last <code>reply</code> so the server will only send back new replies?</p> <p>Also, do I have to manually append everything inside of the callback function of PeriodicalUpdater? Or is there a way to render the <code>show</code> partial for the updates?</p> <p><strong>Update</strong>: I did like @Spencer suggested below, changing it to first have a button that you push to do an AJAX update.</p> <p>I'm having trouble passing that parameter. My actual code looks up things based on a unique URL key, so I just have:</p> <pre><code>&lt;input type="button" value="Refresh" onclick="getReplies('&lt;%= @post.url_key %&gt;');" /&gt; </code></pre> <p>Then in my application.js file:</p> <pre><code>function getReplies(url_key) { $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", dataType: "json", url: "/replies", data: { url_key: url_key }, success: function(msg) { $.each(msg.Replies, function(index, reply) { $("#replies").prepend('&lt;p&gt;' + reply.body + '&lt;/p&gt;') }); } }); } </code></pre> <p>In my controller, I have:</p> <pre><code>def replies @post = Post.find_by_url_key(params[:url_key]) respond_to do |format| format.json { render :json =&gt; @post.replies } end end </code></pre> <p>I get:</p> <pre><code>Processing by PostsController#replies as JSON Parameters: {"_json"=&gt;"url_key=some-post"} ←[1m←[35mPost Load (1.0ms)←[0m SELECT `posts`.* FROM `posts` WHERE (`posts`.`url_key` IS NULL) LIMIT 1 </code></pre> <p>So it looks like the parameter isn't being retrieved from <code>params</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload