0

Railscastsエピソード#229のものと非常によく似たRails 3アプリケーションがあります。唯一の違いは、私のコードではArticlesがPostsと呼ばれ、ネストされたルートがあることです。

ルート.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)

post_controller.rb:8:

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

Comments_Controllerインデックスメソッド:

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

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)

2番目の編集:comments / 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
4

1 に答える 1

2

1. getScript 呼び出しを確認する

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

この行は、コメントのフラット ルートを参照します。多分それは

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

2. ビューを確認します。

このエラーActiveRecord::RecordNotFound (Couldn't find Post with ID=comments)は、このセットアップで間違っているのdata-idは、レンダリングされたビューで終わる の属性である可能性があることを示しています。id投稿の である必要があります。erbファイルも共有していただければ、さらに詳しく知ることができます。

于 2010-09-13T00:56:42.480 に答える