Reputation: 81
I have a problem with my not-working mailer. I am trying to send message on comment reply.
These are my files:
class CommentMailer < ActionMailer::Base
default from: "[email protected]"
def comment_reply(comment, article)
@user = comment.parent.user
@comment = comment.parent
@commenter = comment.user
@url = polymorphic_url(article)
mail(to: @user.email, subject: 'You got a reply to your comment')
end
end
def create
@article = (params[:comment][:article_type]).constantize.find(params[:comment][:article_id])
@comment = @article.comments.build(body: params[:comment][:body], user_id: current_user.id, parent_id: params[:comment][:parent_id])
respond_to do |format|
if @comment.save
if @comment.parent
CommentMailer.comment_reply(@comment, @article).deliver
end
format.js
format.html do
redirect_to polymorphic_path(@article)
end
else
format.js { render 'shared/field_with_errors', resource: @comment }
format.html do
redirect_to polymorphic_path(@article)
end
end
end
end
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 465,
authentication: :login,
enable_starttls_auto: true,
ssl: true,
user_name: ENV["email_login"], #same as default from in mailer
password: ENV["email_password"]
}
<p>Welcome, <%= @user.full_name %></p><br>
<p>You receive this message because <%= @commenter.full_name %> has replied to your comment:</p>
<p><%= @comment.body %></p><br>
<p>You can see the reply here:</p>
<p><%= link_to @url, @url %></p>
create.js.erb works, it just appends a list with rendered comment
When I click button to submit a form with a comment that doesn't have parent it's being added properly. Problem starts when I try to submit a form with a comment that has parent: I click and nothing happen. After page refresh comment appears on the page but no email is sent.
How should I fix it? Thanks
@Baloo, my log:
# this is successful cummit - without parent
Started POST "/en/comments" for 127.0.0.1 at 2014-07-12 08:17:34 +0200
Processing by CommentsController#create as JS
Parameters: {"utf8"=>"✓", "comment"=>{"article_type"=>"Post", "article_id"=>"1", "parent_id"=>"", "body"=>"ok"}, "commit"=>"Post Comment", "locale"=>"en"}
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
Post Load (0.3ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT 1 [["id", 1]]
(0.2ms) BEGIN
SQL (0.3ms) INSERT INTO "comments" ("article_id", "article_type", "body", "created_at", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["article_id", 1], ["article_type", "Post"], ["body", "ok"], ["created_at", "2014-07-12 06:17:34.317645"], ["updated_at", "2014-07-12 06:17:34.317645"], ["user_id", 1]]
(48.6ms) COMMIT
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
default_url_options is passed options: {}
Rendered comments/_comment.html.erb (5.8ms)
Rendered comments/create.js.erb (8.4ms)
Completed 200 OK in 73ms (Views: 14.3ms | ActiveRecord: 50.8ms)
# this is rendering form for reply
Started GET "/en/comments/new?parent_id=123&resource_id=1&resource_type=Post" for 127.0.0.1 at 2014-07-12 08:17:57 +0200
Processing by CommentsController#new as JS
Parameters: {"parent_id"=>"123", "resource_id"=>"1", "resource_type"=>"Post", "locale"=>"en"}
User Load (1.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
Post Load (0.4ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT 1 [["id", 1]]
Comment Load (0.3ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = $1 LIMIT 1 [["id", 123]]
Comment Load (0.3ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = $1 LIMIT 1 [["id", 123]]
CACHE (0.0ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = $1 LIMIT 1 [["id", 123]]
CACHE (0.0ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = $1 LIMIT 1 [["id", 123]]
CACHE (0.0ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = $1 LIMIT 1 [["id", 123]]
CACHE (0.0ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = $1 LIMIT 1 [["id", 123]]
default_url_options is passed options: {}
Rendered comments/_form.html.erb (2.0ms)
CACHE (0.0ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = $1 LIMIT 1 [["id", 123]]
Rendered comments/new.js.erb (9.5ms)
Completed 200 OK in 22ms (Views: 13.7ms | ActiveRecord: 2.3ms)
# this is unsuccessful cummit - reply with parent
Started POST "/en/comments" for 127.0.0.1 at 2014-07-12 08:18:35 +0200
Processing by CommentsController#create as JS
Parameters: {"utf8"=>"✓", "comment"=>{"article_type"=>"Post", "article_id"=>"1", "parent_id"=>"123", "body"=>"come on"}, "commit"=>"Post Comment", "locale"=>"en"}
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
Post Load (2.0ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT 1 [["id", 1]]
Comment Load (2.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = $1 LIMIT 1 [["id", 123]]
(3.0ms) BEGIN
SQL (1.1ms) INSERT INTO "comments" ("ancestry", "article_id", "article_type", "body", "created_at", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["ancestry", "123"], ["article_id", 1], ["article_type", "Post"], ["body", "come on"], ["created_at", "2014-07-12 06:18:35.288487"], ["updated_at", "2014-07-12 06:18:35.288487"], ["user_id", 1]]
(46.3ms) COMMIT
Comment Load (0.4ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = $1 LIMIT 1 [["id", 123]]
CACHE (0.0ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = $1 LIMIT 1 [["id", 123]]
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
CACHE (0.0ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = $1 LIMIT 1 [["id", 123]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
CommentMailer#comment_reply: processed outbound mail in 6.0ms
Completed 500 Internal Server Error in 81ms
ActionController::UrlGenerationError (No route matches {:action=>"show", :controller=>"posts", :format=>nil, :id=>nil, :locale=>#<Post id: 1, user_id: 1, published: true, created_at: "2014-07-01 11:32:20", updated_at: "2014-07-04 10:48:12", seo_description: "", slug: "title", keywords: [], en_title: "TITLE en", es_title: "TITLE es", en_body: "jasnd jnaksd nkajsnd kajnskd jnakjnskjndfajnksj fn...", es_body: "es body es es es", body: nil, title: nil>} missing required keys: [:id]):
app/mailers/comment_mailer.rb:8:in `comment_reply'
app/controllers/comments_controller.rb:22:in `block in create'
app/controllers/comments_controller.rb:19:in `create'
Rendered /home/bartek/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.9ms)
Rendered /home/bartek/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.9ms)
Rendered /home/bartek/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (1.1ms)
Rendered /home/bartek/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb (16.9ms)
Upvotes: 0
Views: 418
Reputation: 81
Working now, I just had to specify locale in url:
@url = polymorphic_url(article, locale: I18n.locale)
Upvotes: 1