2014-11-20 68 views
0

所以,我有一個通訊應用程序,我從頭開始構建,並試圖實現取消訂閱功能。取消訂閱方法拋出一個未定義的方法update_attributes錯誤

但是,當用戶單擊取消訂閱時,日誌顯示NilClass的未定義方法update_attributes錯誤。

我在做什麼錯?

subscribers_controller.rb

class SubscribersController < ApplicationController  
    def new 
    @subscriber = Subscriber.new 
    end 

    def create 
    @subscriber = Subscriber.new(subscriber_params) 

    if @subscriber.save 
     redirect_to root_path 
    else 
     render :new 
    end 
    end 

    def unsubscribe 
    subscriber = Subscriber.find_by_unsubscribe_hash(params[:unsubscribe_hash]) 
    if subscriber.update_attributes(:subscription, false) 
     redirect_to root_path 
    else 
     flash[:notice] = "Error while un-subscribing, please try again" 
    end 
    end 

    private 

    def subscriber_params 
    params.require(:subscriber).permit(:email, :category_id, :created_at, :updated_at, :category => {}, 
             category_attributes: [:id, :name, :category_type, :created_at, :updated_at]) 
    end 

    def permitted_params 
    params.permit(:email, :category_id, :created_at, :updated_at, :category => {}, 
             category_attributes: [:id, :name, :category_type, :created_at, :updated_at]) 
    end 

    def attr_params_category 
    params.require(:account).permit(category_attributes: [:id, :name, :category_type, :created_at, :updated_at, :category => {}]) 
    end 

end 

subscriber.rb

class Subscriber < ActiveRecord::Base 
    before_create :add_unsubscribe_hash 

    belongs_to :category 
    has_many :quote_histories 

    validates :email, presence: true 
    validates :category_id, presence: true 

    def choose_quote(cat) 
    quote = cat.quotes.uniq_quote_for(self.id).order("RANDOM()").first 
    return nil if quote.nil? # so if we didn't able to find in quote lets return from here with nil!k 
    return quote if self.quote_histories.create quote ande(quote_id: quote.id) # so here we won't need to test for nil! 
    #so this will reduce the time to perform this operation 
    end 

    private 

    def add_unsubscribe_hash 
    self.unsubscribe_hash = SecureRandom.hex 
    end 
end 

schema.rb

create_table "subscribers", force: true do |t| 
    t.string "email" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.integer "category_id" 
    t.string "unsubscribe_hash" 
    end 

的routes.rb

get 'subscribers/unsubscribe/:unsubscribe_hash' => 'subscribers#unsubscribe', :as => 'unsubscribe' 

,然後在郵件視圖模板:

<span><%= link_to 'un-subscribe', unsubscribe_url(@subscriber.unsubscribe_hash) %></span> 

調試日誌

2014-11-20T22:17:15.937062+00:00 heroku[router]: at=info method=GET path="/subscribers/unsubscribe/cbdb9b40962f1cc6b475096b73c6d5fd" host=motiv8.io request_id=981ed2f0-390e-4d33-888f-26aa8c8060ff fwd="99.234.25.253" dyno=web.1 connect=5ms service=11ms status=500 bytes=1754 
2014-11-20T22:17:15.927014+00:00 app[web.1]: Started GET "/subscribers/unsubscribe/cbdb9b40962f1cc6b475096b73c6d5fd" for 99.234.25.253 at 2014-11-20 22:17:15 +0000 
2014-11-20T22:17:15.932546+00:00 app[web.1]: 
2014-11-20T22:17:15.932548+00:00 app[web.1]: RuntimeError ("cbdb9b40962f1cc6b475096b73c6d5fd"): 
2014-11-20T22:17:15.932550+00:00 app[web.1]: app/controllers/subscribers_controller.rb:32:in `unsubscribe' 
2014-11-20T22:17:15.932552+00:00 app[web.1]: 
2014-11-20T22:17:15.932553+00:00 app[web.1]: 
2014-11-20T22:17:15.930282+00:00 app[web.1]: Processing by SubscribersController#unsubscribe as HTML 
2014-11-20T22:17:15.930302+00:00 app[web.1]: Parameters: {"unsubscribe_hash"=>"cbdb9b40962f1cc6b475096b73c6d5fd"} 
2014-11-20T22:17:15.931362+00:00 app[web.1]: Completed 500 Internal Server Error in 1ms 

和導軌C:

Subscriber.find_by_unsubscribe_hash("cbdb9b40962f1cc6b475096b73c6d5fd") 
    Subscriber Load (2.2ms) SELECT "subscribers".* FROM "subscribers" WHERE "subscribers"."unsubscribe_hash" = 'cbdb9b40962f1cc6b475096b73c6d5fd' LIMIT 1 
    Subscriber Load (2.2ms) SELECT "subscribers".* FROM "subscribers" WHERE "subscribers"."unsubscribe_hash" = 'cbdb9b40962f1cc6b475096b73c6d5fd' LIMIT 1 
=> nil 

回答

1

看看您的取消訂閱操作。

更換

if subscriber.update_attributes(:subscription, false) 

if subscriber.update_attribute(:subscription, false) 

這裏#update_attributes可以利用哈希像{訂閱:假},但你通過兩種不同說法。因此,類似的解決方案是:

if subscriber.update_attributes({subscription: false}) 

不管怎麼說,我的退訂動作的代碼一點點整個街區更新。

def unsubscribe 
    subscriber = Subscriber.find_by_unsubscribe_hash(params[:unsubscribe_hash]) 
    if subscriber and subscriber.update_attribute(:subscription, false) 
    redirect_to root_path 
    elsif subscriber.nil? 
    flash[:notice] = "Subscription not found" 
    else 
    flash[:notice] = "Error while un-subscribing, please try again" 
    end 
end 
相關問題