Rails 4.0.2
当有人创建评论时,我正在使用rails observer发送邮件。 这是我的观察者 -
class CommentObserver < ActiveRecord::Observer
include Users::AdminsHelper
def after_create(comment)
commenter = get_user(comment.user_id)
receiver = get_receiver(commenter)
NotificationMailer.new_comment_email(receiver,commenter).deliver
end
def get_receiver(user)
#user = get_user(comment.user_id)
unless user.mentors.empty?
user.mentors.first
else
user = get_user_by_update_id(comment.update_id)
end
end
end
现在我有两种类型的用户导师和受指导者。因此,当受指导者创建评论时,一切正常,但当导师创建评论时,我会收到错误。
这是我为两个用户呈现的部分内容 -
<div class="mentee-updates">
<h1>Updates</h1>
<% @previous_updates.each do |update| %>
<div class="post">
<div class="heading">
<div class="user">
<%= get_user(update.user_id).name %>
</div>
<div class="time">
<%= "#{time_ago_in_words(update.created_at)} ago" %>
</div>
</div>
<div class="clear"></div>
<hr/>
<div class="update">
<%= update.content %>
</div>
</br>
<% unless update.comments.empty? %>
<% update.comments.sort.each do |comment| %>
<div class="comment-area">
<div class="user">
User <%= comment.user_id%>
<% if current_user.id == comment.user_id then %>
<%= link_to image_tag("DeleteRed.png"), comments_path(comment,update_id: update.id), remote: true, method: :delete, data: { confirm: 'Are you sure?' } ,class: 'delete_icon'%>
<% end %>
</div>
<div class="text-muted">
<%= time_ago_in_words(comment.created_at)%>
</div>
<div class="comment">
<%= comment.content %>
</div>
</div>
<% end %>
<% end %>
</br>
<% if current_user.has_role? :associate then %>
<%= render partial: "comment", locals: { comment: @comment, update: update } %>
<% end %>
</div>
<% end %>
</div>
这是我的评论部分 -
<div class="comment_form">
<%= form_for(@comment, remote: true,method: :post ) do |f| %>
<%= hidden_field_tag 'update_id', update.id %>
<%= f.label 'Add Comment'%>
<%= f.text_area :content, id: 'comment' %>
<%= f.submit "Add", class: "btn btn-sm" %>
<% end %></br>
</div>
因为它适用于某种类型的用户,我不认为部分用户有任何问题。
错误讯息 -
Started POST "/comments" for 127.0.0.1 at 2014-04-22 15:32:15 +0530
Processing by CommentsController#create as JS
Parameters: {"utf8"=>"✓", "update_id"=>"9", "comment"=>{"content"=>",jhjnhj"}, "commit"=>"Add"}
[1m[35mUpdate Load (1.0ms)[0m SELECT "updates".* FROM "updates" WHERE "updates"."id" = 9 ORDER BY created_at DESC LIMIT 1
[1m[36mUser Load (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = 4 ORDER BY "users"."id" ASC LIMIT 1[0m
[1m[35m (0.0ms)[0m begin transaction
[1m[36mSQL (1.0ms)[0m [1mINSERT INTO "comments" ("content", "created_at", "update_id", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?)[0m [["content", ",jhjnhj"], ["created_at", Tue, 22 Apr 2014 10:02:15 UTC +00:00], ["update_id", 9], ["updated_at", Tue, 22 Apr 2014 10:02:15 UTC +00:00], ["user_id", 4]]
[1m[35mUser Load (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = 4 LIMIT 1
[1m[36mUser Exists (0.0ms)[0m [1mSELECT 1 AS one FROM "users" INNER JOIN "mentees_mentors" ON "users"."id" = "mentees_mentors"."mentor_id" WHERE "mentees_mentors"."mentee_id" = ? LIMIT 1[0m [["mentee_id", 4]]
[1m[35m (1.0ms)[0m rollback transaction
Completed 500 Internal Server Error in 11ms
NameError - undefined local variable or method `comment' for #<CommentObserver:0x40f59f0>:
app/models/comment_observer.rb:22:in `get_receiver'
app/models/comment_observer.rb:13:in `after_create'
rails-observers (0.1.2) lib/rails/observers/active_model/observing.rb:352:in `update'
rails-observers (0.1.2) lib/rails/observers/activerecord/observer.rb:118:in `block (2 levels) in define_callbacks'
activesupport (4.0.2) lib/active_support/callbacks.rb:375:in `_run__943343009__create__callbacks'
activesupport (4.0.2) lib/active_support/callbacks.rb:80:in `run_callbacks'
activerecord (4.0.2) lib/active_record/callbacks.rb:306:in `create_record'
activerecord (4.0.2) lib/active_record/timestamp.rb:57:in `create_record'
activerecord (4.0.2) lib/active_record/persistence.rb:477:in `create_or_update'