我不确定为什么我会收到以下错误。我是rails的新手,所以非常感谢任何帮助和解释。
after_create :send_email_to_subscribers
send_email_to_subscribers
ActionMailer
after_filter :send_email_to_subscribers
而不是after_create :send_email_to_subscribers
,但这会在mailer_subscribe.rb文件中引发更多错误以下是我得到的当前错误:
博客控制器的未定义方法`after_create':Class
终端显示错误:
Started GET "/useras/1/blogs/2" for 127.0.0.1 at 2015-06-20 20:14:16 +0100
ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by BlogsController#show as HTML
Parameters: {"usera_id"=>"1", "id"=>"2"}
Blog Load (0.1ms) SELECT "blogs".* FROM "blogs" WHERE "blogs"."id" = ? LIMIT 1 [["id", 2]]
Usera Load (0.1ms) SELECT "useras".* FROM "useras" WHERE "useras"."id" = ? LIMIT 1 [["id", 1]]
Blog Load (0.2ms) SELECT "blogs".* FROM "blogs" WHERE "blogs"."usera_id" = ? AND "blogs"."id" = ? LIMIT 1 [["usera_id", 1], ["id", 2]]
Rendered blogs/show.html.erb within layouts/application (3.0ms)
Subscriber Load (0.2ms) SELECT "subscribers".* FROM "subscribers"
MailerSubscribe#subscription_email: processed outbound mail in 7.8ms
Completed 500 Internal Server Error in 891ms (Views: 798.5ms | ActiveRecord: 1.4ms)
NoMethodError - undefined method `title' for #<BlogsController:0x007fae0af874f0>:
app/mailers/mailer_subscribe.rb:6:in `subscription_email'
actionpack (4.1.10) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.1.10) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.1.10) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.10) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.1.10) lib/abstract_controller/base.rb:136:in `process'
blogs_controller.rb
class BlogsController < ApplicationController
respond_to :html, :xml, :json
before_action :set_blog, only: [:show, :edit, :update, :destroy]
after_create :send_email_to_subscribers
def index
...
end
def show
...
end
def new
@usera = Usera.find(params[:usera_id])
@blog = @usera.blogs.build
respond_with(@blog)
end
def edit
end
def create
@usera = Usera.find(params[:usera_id])
@blog = @usera.blogs.create(blog_params)
respond_to do |format|
if @blog.save
format.html { redirect_to([@blog.usera, @blog], notice: 'Blog was successfully created.') }
format.json { render json: @blog, status: :created, location: @blog }
else
format.html { render action: "new" }
format.json { render json: @blog.errors, status: :unprocessable_entity }
end
end
end
def update
...
end
def destroy
...
end
private
def set_blog
@blog = Blog.find(params[:id])
end
def blog_params
params.require(:blog).permit(:title, :content, :usera_id, :link, :image, :category_blog_id, :image)
end
def send_email_to_subscribers
Subscriber.all.each do |subscriber|
MailerSubscribe.subscription_email(subscriber.email,self)
end
end
end
mailer_subscribe.rb
class MailerSubscribe < ActionMailer::Base
default from: "info@recruitmentafrica"
def subscription_email(email,blog)
@blog = blog
mail(to: email, subject: @blog.title)
end
end
答案 0 :(得分:7)
after_create
是模型回调。不是控制器回调。
如果你想添加一个在你的create方法之后运行的回调,你可以这样做:
after_action :send_email_to_subscribers, only: [:create]
但,这仍然会在发送响应之前发生,并会缩短您的响应时间!您应该考虑使用后台流程,为此目的,有几个宝石,如Sidekiq和Rescue。
答案 1 :(得分:3)
after_create
是ActiveRecord::Base
上的类方法,因此是模型 - 它与控制器无关。
此外,您应该将邮件传递移动到异步进程。您不希望通过邮件处理捆绑请求/响应周期,这可能会消耗不确定的时间。