我很难理解为什么我不能使用第一种方法渲染部分而不是第二种方法的原因。将我的代码更改为<%= render partial: @article.reload.comments %>
有效。评论表是一个用Ajax提交的远程表单。
我在文章显示页面上为nil Class获取了一个未定义的方法'email'(我为用户设置了gravatar),下面的行突出显示。
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
此错误消息仅在我通过Ajax提交一些评论后刷新文章显示页面时显示。
提交评论表单时Rails控制台:
Started POST "/articles/et-incidunt-labore/comments"
Processing by CommentsController#create as JS
Parameters: {"utf8"=>"✓", "comment"=>{"content"=>"this is a new comment"}, "commit"=>"Add Comment", "article_id"=>"et-incidunt-labore"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Article Load (0.4ms) SELECT "articles".* FROM "articles" WHERE "articles"."slug" = $1 LIMIT $2 [["slug", "et-incidunt-labore"], ["LIMIT", 1]]
(0.1ms) BEGIN
SQL (0.6ms) INSERT INTO "comments" ("content", "article_id", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["content", "this is a new comment"], ["article_id", 154], ["user_id", 1], ["created_at", "2017-11-04 11:42:10.099567"], ["updated_at", "2017-11-04 11:42:10.099567"]]
(2.3ms) COMMIT
Rendering comments/create.js.erb
Rendered comments/_comment.html.erb (1.4ms)
(7.7ms) SELECT COUNT(*) FROM "comments" WHERE "comments"."article_id" = $1 [["article_id", 154]]
Rendered comments/create.js.erb (19.0ms)
Completed 200 OK in 47ms (Views: 18.4ms | ActiveRecord: 15.0ms)
重新加载文章显示页面时Rails控制台:
Started GET "/articles/et-incidunt-labore"
Processing by ArticlesController#show as HTML
Parameters: {"id"=>"et-incidunt-labore"}
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Article Load (1.0ms) SELECT "articles".* FROM "articles" WHERE "articles"."slug" = $1 LIMIT $2 [["slug", "et-incidunt-labores"], ["LIMIT", 1]]
(0.2ms) BEGIN
Article Load (0.3ms) SELECT "articles".* FROM "articles" WHERE "articles"."id" = $1 LIMIT $2 [["id", 154], ["LIMIT", 1]]
Rendering articles/show.html.erb within layouts/application
Comment Exists (0.4ms) SELECT 1 AS one FROM "comments" WHERE "comments"."article_id" = $1 LIMIT $2 [["article_id", 154], ["LIMIT", 1]]
Comment Load (0.3ms) SELECT "comments".* FROM "comments" WHERE "comments"."article_id" = $1 [["article_id", 154]]
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Rendered collection of comments/_comment.html.erb [2 times] (5.9ms)
Rendered articles/show.html.erb within layouts/application (27.5ms)
Completed 500 Internal Server Error in 80ms (ActiveRecord: 32.9ms)
ActionView::Template::Error (undefined method `email' for nil:NilClass):
3: <div class="card-block">
4: <div class="row">
5: <div class="col-md-1 col-xs-2">
6: <%= gravatar_for(comment.user, size:50) %>
7: </div>
8: <div class="col-md-11 col-xs-10">
9: <b><%= comment.user.name %></b>
app/helpers/users_helper.rb:4:in `gravatar_for'
app/views/comments/_comment.html.erb:6:in `_app_views_comments__comment_html_erb___3274541922963352908_69873959519460'
app/views/articles/show.html.erb:57:in `_app_views_articles_show_html_erb___4339274147925900264_69874017658060'
app/controllers/application_controller.rb:17:in `catch_not_found'
物品/ show.html.erb
第一种方法:
<% if @comments.exists? %>
<%= render @comments %>
<% else %>
<p> There are no comments yet.</p>
<% end %>
第二种方法:
<% if @comments.exists? %>
<%= render partial: @article.reload.comments %>
<% else %>
<p> There are no comments yet.</p>
<% end %>
文章控制器
def show
@article = Article.friendly.find(params[:id])
@comments = @article.comments
@new_comment = @article.comments.new
end
评论/ comment.html.erb
<%= comment.content %>
可以在我发布的this other Stackoverflow question上找到与此评论功能相关的其余代码。