我如何获得 Railscasts 剧集“#229:投票更改”使用嵌套路由?
我有一个 Rails 3 应用程序,与 Railscasts Episode #229 中的应用程序非常相似 唯一的区别是,在我的代码中,Articles 被称为 Posts,并且我有一个嵌套路由:
routes.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 index method:
def index @comments = Comment.where("post_id = ? andcreated_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>
comments/_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)) %>");
评论_controller.rb:
def show
@comments = Comment.where("post_id = ? and created_at > ?", params[:post_id], Time.at(params[:after].to_i + 1))
end
I have a rails 3 application very similar to the one in Railscasts episode #229 the only difference is that in my code Articles is called Posts and I have a nested route:
routes.rb:
Myapp::Application.routes.draw do
resources :posts do
resources :comments
end
root :to => "tags#index"
end
I receive this error in the terminal:
[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 index method:
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);
}
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?
EDIT:
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>
comments/_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 %>
After changing the line mentioned in application.js to:
$.getScript("/posts/" + post_id + "/comments/&after=" + after)
I get the error in my terminal:
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)
SECOND EDIT:
comments/index.js.erb:
<% unless @comments.empty? %>
$("#comments").append("<%=raw escape_javascript(render(@comments)) %>");
<% end %>
comments/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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1.检查getScript调用
这一行指的是注释的扁平路由。也许应该像
2。检查你的观点。
错误
ActiveRecord::RecordNotFound (Couldn't find Post with ID=comments)
告诉我此设置中的错误可能是该内容上的data-id
属性最终出现在您的渲染视图中。它应该是帖子的id
。如果您也分享 erb 文件,我可以告诉您更多信息。1. Check the getScript call
This line refers to a flat route for comments. Maybe it should be like
2. Check your views.
The error
ActiveRecord::RecordNotFound (Couldn't find Post with ID=comments)
tells me that the incorrect thing in this setup may be thedata-id
attribute on the that ends up in your rendered view. It should be theid
of the post. I can tell more if you share the erb file as well.