2015-06-20 84 views
2

我不確定爲什麼我得到下面的錯誤。我是新來的鐵路所以任何幫助和解釋將不勝感激。控制器未定義的方法`after_create':類

  1. 我想每次創建博客時都向用戶發送電子郵件。
  2. 在我的博客控制器我在我的博客控制器添加after_create :send_email_to_subscribers
  3. 我也加入了該方法send_email_to_subscribers
  4. 我已經到位,成功ActionMailer
  5. 這個最初工作,突然停了下來
  6. 有after_create方法已被解除推翻?
  7. 錯誤提示,當我點擊<%=的link_to「創建新博客」,new_usera_blog_path(current_usera)%>
  8. 我嘗試使用,而不是在我的博客控制器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 

回答

6

after_create是模型回調。不是控制器回調。

如果你想添加這是您的創建方法後運行的回調,你會做:

after_action :send_email_to_subscribers, only: [:create] 

,發送前的響應,並會減慢你的響應時間,這仍然出現!你應該考慮使用後臺進程,爲此目的有幾個像SidekiqRescue的寶石。

+0

非常感謝!!!!解決了錯誤 - 謝謝你喲!我聽說過關於sidekiq和救援,但對他們不太瞭解,謝謝你的建議 - 肯定會進一步閱讀並實施它們 - 謝謝 – ARTLoe

3

after_createActiveRecord::Base,因此模型類方法 - 它無關控制器。

此外,您應該將郵件傳遞移動到異步進程。您不希望通過可能消耗不確定時間的郵件處理來綁定請求/響應週期。