我不確定爲什麼我得到下面的錯誤。我是新來的鐵路所以任何幫助和解釋將不勝感激。控制器未定義的方法`after_create':類
- 我想每次創建博客時都向用戶發送電子郵件。
- 在我的博客控制器我在我的博客控制器添加
after_create :send_email_to_subscribers
- 我也加入了該方法
send_email_to_subscribers
- 我已經到位,成功
ActionMailer
- 這個最初工作,突然停了下來
- 有after_create方法已被解除推翻?
- 錯誤提示,當我點擊<%=的link_to「創建新博客」,new_usera_blog_path(current_usera)%>
- 我嘗試使用,而不是在我的博客控制器
after_create :send_email_to_subscribers
但提示了在mailer_subscribe更多的錯誤。 RB文件
的下面是當前錯誤我得到:
undefined method `after_create' for BlogsController: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: "[email protected]"
def subscription_email(email,blog)
@blog = blog
mail(to: email, subject: @blog.title)
end
end
非常感謝!!!!解決了錯誤 - 謝謝你喲!我聽說過關於sidekiq和救援,但對他們不太瞭解,謝謝你的建議 - 肯定會進一步閱讀並實施它們 - 謝謝 – ARTLoe