Note that there are some explanatory texts on larger screens.

plurals
  1. POHow I get the Railscasts episode "#229: Polling for Changes" to work with a nested route?
    primarykey
    data
    text
    <p>I have a rails 3 application very similar to the one in <a href="http://github.com/ryanb/railscasts-episodes/tree/master/episode-229" rel="nofollow noreferrer">Railscasts episode #229</a> the only difference is that in my code Articles is called Posts and I have a nested route:</p> <p>routes.rb:</p> <pre><code>Myapp::Application.routes.draw do resources :posts do resources :comments end root :to =&gt; "tags#index" end </code></pre> <p>I receive this error in the terminal:</p> <pre><code>[2010-09-13 00:22:13] ERROR NoMethodError: undefined method `page_cache_extension' for ActionController::Base:Class /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.0/lib/action_dispatch/middleware/static.rb:21:in `call' /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.0/lib/rails/application.rb:168:in `call' /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.0/lib/rails/application.rb:77:in `method_missing' /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.0/lib/rails/rack/log_tailer.rb:14:in `call' /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/content_length.rb:13:in `call' /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/handler/webrick.rb:52:in `service' /usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service' /usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run' /usr/local/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread' Started GET "/posts/comments.js?post_id=1&amp;after=1284333896" for 192.168.1.108 at 2010-09-13 00:22:15 +0000 Processing by PostsController#show as JS Parameters: {"post_id"=&gt;"1", "after"=&gt;"1284333896", "id"=&gt;"comments"} SQL (4.8ms) SELECT name FROM sqlite_master WHERE type = 'table' AND NOT name = 'sqlite_sequence' SQL (1.5ms) SELECT name FROM sqlite_master WHERE type = 'table' AND NOT name = 'sqlite_sequence' Post Load (0.7ms) SELECT "posts".* FROM "posts" WHERE ("posts"."id" = 0) LIMIT 1 Completed in 392ms ActiveRecord::RecordNotFound (Couldn't find Post with ID=comments): app/controllers/posts_controller.rb:8:in `show' Rendered /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (11.4ms) Rendered /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (49.3ms) Rendered /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (92.4ms) </code></pre> <p>posts_controller.rb:8:</p> <pre><code>@post = Post.find(params[:id]) </code></pre> <p>Comments_Controller index method:</p> <p>def index @comments = Comment.where("post_id = ? and created_at > ?", params[:post_id], Time.at(params[:after].to_i + 1)) end</p> <p>application.js:</p> <pre><code>$(function() { if ($("#comments").length &gt; 0) { setTimeout(updateComments, 10000); } }); function updateComments () { var post_id = $("#post").attr("data-id"); if ($("#comments").length &gt; 0) { var after = $(".comment:last-child").attr("data-time"); } else { var after = "0"; } $.getScript("/comments.js?post_id=" + post_id + "&amp;after=" + after) setTimeout(updateComments, 10000); } </code></pre> <p>I have a hunch that the problem is the nested route. How do I get the Javascript in application.js to recognize the nested route?</p> <p>EDIT:</p> <p>posts/show.html.erb:</p> <pre><code>&lt;div id="post" data-id="&lt;%= @post.id %&gt;"&gt; &lt;%= link_to @post.title, @post %&gt; &lt;%= simple_format @post.content %&gt; &lt;p&gt;&lt;%= link_to "Back to Posts", posts_path %&gt;&lt;/p&gt; &lt;% unless @post.comments.empty? %&gt; &lt;h2&gt;&lt;%= pluralize(@post.comments.size, 'comment') %&gt;&lt;/h2&gt; &lt;div id="comments"&gt; &lt;%= render @post.comments %&gt; &lt;/div&gt; &lt;% end %&gt; &lt;/div&gt; &lt;div id="replyform"&gt; &lt;%= render "comments/form" %&gt; &lt;/div&gt; </code></pre> <p>comments/_form.html.erb:</p> <pre><code>&lt;%= form_for([@post, Comment.new], :html =&gt; { :multipart =&gt; true }) do |f| %&gt; &lt;fieldset&gt; &lt;fieldset id="optional"&gt; &lt;%= f.label :commenter, "name (optional)" %&gt; &lt;%= f.text_field :commenter, :placeholder =&gt; "name (optional)" %&gt; &lt;%= f.label :email, "email (optional)" %&gt; &lt;%= f.email_field :email, :placeholder =&gt; "email (optional)" %&gt; &lt;/fieldset&gt; &lt;/fieldset&gt; &lt;fieldset&gt; &lt;%= f.label :body, "reply " %&gt; &lt;%= f.text_area :body, :placeholder =&gt; "reply" %&gt; &lt;%= f.submit 'reply' %&gt; &lt;/fieldset&gt; &lt;% end %&gt; </code></pre> <p>After changing the line mentioned in application.js to:</p> <pre><code>$.getScript("/posts/" + post_id + "/comments/&amp;after=" + after) </code></pre> <p>I get the error in my terminal:</p> <pre><code>Started GET "/posts/1/comments/&amp;after=1284388076" for 192.168.1.108 at 2010-09-13 14:28:29 +0000 Processing by CommentsController#show as JS Parameters: {"post_id"=&gt;"1", "id"=&gt;"&amp;after=1284388076"} Completed in 28ms ActionView::MissingTemplate (Missing template comments/show with {:handlers=&gt;[:erb, :rjs, :builder, :rhtml, :rxml], :formats=&gt;[:js, :"*/*"], :locale=&gt;[:en, :en]} in view paths "/media/usb0/myapp/app/views"): Rendered /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.0/lib/action_dispatch/middleware/templates/rescues/missing_template.erb within rescues/layout (5.9ms) </code></pre> <p>SECOND EDIT: comments/index.js.erb: </p> <pre><code>&lt;% unless @comments.empty? %&gt; $("#comments").append("&lt;%=raw escape_javascript(render(@comments)) %&gt;"); &lt;% end %&gt; </code></pre> <p>comments/show.js.erb:</p> <pre><code>$("#comments").append("&lt;%=raw escape_javascript(render(@comments)) %&gt;"); </code></pre> <p>comments_controller.rb:</p> <pre><code>def show @comments = Comment.where("post_id = ? and created_at &gt; ?", params[:post_id], Time.at(params[:after].to_i + 1)) end </code></pre>
    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.
    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