javascript - 如何让 Railscasts 剧集 "#229: Polling for Changes"使用嵌套路线?

标签 javascript ruby-on-rails ruby-on-rails-3 unobtrusive-javascript nested-routes

我有一个与 Railscasts episode #229 中的应用程序非常相似的 Rails 3 应用程序唯一的区别是,在我的代码中,文章称为帖子,并且我有一个嵌套路由:

路线.rb:

Myapp::Application.routes.draw do
  resources :posts do
    resources :comments
  end
  root :to => "tags#index"
end

我在终端中收到此错误:

[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&after=1284333896" for 192.168.1.108 at 2010-09-13 00:22:15 +0000
        Processing by PostsController#show as JS
        Parameters: {"post_id"=>"1", "after"=>"1284333896", "id"=>"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)

posts_controller.rb:8:

@post = Post.find(params[:id])

Comments_Controller索引方法:

定义索引 @comments = Comment.where("post_id = ? andcreated_at > ?", params[:post_id], Time.at(params[:after].to_i + 1)) 结束

application.js:

$(function() {
  if ($("#comments").length > 0) {
    setTimeout(updateComments, 10000);
  }
});

function updateComments () {
  var post_id = $("#post").attr("data-id");
  if ($("#comments").length > 0) {
    var after = $(".comment:last-child").attr("data-time");
  } else {
    var after = "0";
  }
  $.getScript("/comments.js?post_id=" + post_id + "&after=" + after)
  setTimeout(updateComments, 10000);
}

我有预感问题出在嵌套路由上。如何让 application.js 中的 Javascript 识别嵌套路由?

编辑:

posts/show.html.erb:

<div id="post" data-id="<%= @post.id %>">
  <%= link_to @post.title, @post %>
  <%= simple_format @post.content %>

  <p><%= link_to "Back to Posts", posts_path %></p>

  <% unless @post.comments.empty? %>
    <h2><%= pluralize(@post.comments.size, 'comment') %></h2>

    <div id="comments">
      <%= render @post.comments %>
    </div>
  <% end %>
</div>

<div id="replyform">
  <%= render "comments/form" %>
</div>

评论/_form.html.erb:

<%= form_for([@post, Comment.new], :html => { :multipart => true }) do |f| %>
  <fieldset>
    <fieldset id="optional">
      <%= f.label :commenter, "name (optional)" %>
      <%= f.text_field :commenter, :placeholder => "name (optional)" %>

      <%= f.label :email, "email (optional)" %>
      <%= f.email_field :email, :placeholder => "email (optional)" %>
    </fieldset>
  </fieldset>
  <fieldset>
    <%= f.label :body, "reply " %>
    <%= f.text_area :body, :placeholder => "reply" %>
    <%= f.submit 'reply' %>
  </fieldset>
<% end %>

将 application.js 中提到的行更改为:

$.getScript("/posts/" + post_id + "/comments/&after=" + after)

我的终端出现错误:

Started GET "/posts/1/comments/&after=1284388076" for 192.168.1.108 at 2010-09-13 14:28:29 +0000
  Processing by CommentsController#show as JS
  Parameters: {"post_id"=>"1", "id"=>"&after=1284388076"}
Completed   in 28ms

ActionView::MissingTemplate (Missing template comments/show with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:js, :"*/*"], :locale=>[: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)

第二次编辑: 评论/index.js.erb:

<% unless @comments.empty? %>
  $("#comments").append("<%=raw escape_javascript(render(@comments)) %>");
<% end %>

评论/show.js.erb:

$("#comments").append("<%=raw escape_javascript(render(@comments)) %>");

comments_controller.rb:

def show
  @comments = Comment.where("post_id = ? and created_at > ?", params[:post_id], Time.at(params[:after].to_i + 1))
end

最佳答案

1。检查 getScript 调用

$.getScript("/comments.js?post_id=" + post_id + "&after=" + after)

这条线是指评论的平坦路线。也许应该是这样的

$.getScript("/posts/" + post_id + "/comments?after=" + after)

2。检查您的观点。

错误ActiveRecord::RecordNotFound(无法找到 ID=comments 的帖子) 告诉我此设置中的错误可能是 data-id 属性最终出现在您的渲染 View 中。它应该是帖子的id。如果您也分享 erb 文件,我可以告诉您更多信息。

关于javascript - 如何让 Railscasts 剧集 "#229: Polling for Changes"使用嵌套路线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3697282/

相关文章:

javascript - Google map InfoWindow 触发器 - 未捕获的 ReferenceError : function is not defined

javascript - 使用 Cypress 测试重定向到新路由

ruby-on-rails - 使用 Rails 3 Hook gem 以在 Rack 堆栈上添加中间件

ruby-on-rails - 如何获取我的 Rails Controller 中存储在事件存储中的附件的 URL

ruby-on-rails - rspec .reload 不重新加载对象

javascript - Highcharts 热图中每个单元格的边框颜色

javascript - 将 cookie 域设置为 IP 地址(使用 CORS)

mysql - 一般事情开发人员必须知道有 2 年以上的经验?

ruby-on-rails-3 - 本地跳转错误 find_each 上没有给出 block (yield)错误

ruby-on-rails - 使用 STI 模型名称解决方法的弃用警告