ruby on rails 4 - RailsObservers - NameError - undefined local variable or method comment for CommentObserver -
rails 4.0.2
i using rails observer send mail when creates comment. 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
now have 2 types of users mentors , mentee. when mentee creates comment, works fine when mentor creates comment error.
this partial render both users-
<div class="mentee-updates"> <h1>updates</h1> <% @previous_updates.each |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 |comment| %> <div class="comment-area"> <div class="user"> user <%= comment.user_id%> <% if current_user.id == comment.user_id %> <%= link_to image_tag("deletered.png"), comments_path(comment,update_id: update.id), remote: true, method: :delete, data: { confirm: 'are 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 %> <%= render partial: "comment", locals: { comment: @comment, update: update } %> <% end %> </div> <% end %> </div>
this comment partial-
<div class="comment_form"> <%= form_for(@comment, remote: true,method: :post ) |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>
as works fine 1 type of user, dont think there problem in partials.
error message-
started post "/comments" 127.0.0.1 @ 2014-04-22 15:32:15 +0530 processing commentscontroller#create js parameters: {"utf8"=>"✓", "update_id"=>"9", "comment"=>{"content"=>",jhjnhj"}, "commit"=>"add"} [1m[35mupdate load (1.0ms)[0m select "updates".* "updates" "updates"."id" = 9 order created_at desc limit 1 [1m[36muser load (0.0ms)[0m [1mselect "users".* "users" "users"."id" = 4 order "users"."id" asc limit 1[0m [1m[35m (0.0ms)[0m begin transaction [1m[36msql (1.0ms)[0m [1minsert "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".* "users" "users"."id" = 4 limit 1 [1m[36muser exists (0.0ms)[0m [1mselect 1 one "users" inner join "mentees_mentors" on "users"."id" = "mentees_mentors"."mentor_id" "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' #<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'
Comments
Post a Comment