2015-09-03 19 views
0

當我GET/admin/consoles/1/edit,例如,出現這種情況:ActiveAdmin沒有一個模型中承認 「自我」

無法與 'ID' 找品牌=

然後它是強調了以下代碼片段,其我在 /app/models/console.rb

def full_name 
    brand = Brand.find(self.brand_id).name 
    "#{brand} #{self.name}" 
end 

好像它是不承認self.brand_id。想法?

+0

並表'consoles'的'brand_id'列其中'ID = 1'包含的值?此錯誤表示此行中的「brand_id」列爲空值。 –

+0

什麼是您的管理控制檯模型?嘗試使用調試器檢查自己。 –

+0

什麼是聯想品牌控制檯? – neo

回答

1

我需要看到你的app/models/console.rb可以肯定的,但似乎你應該有一個belongs_to關係,那麼你可以只使用這種關係......像這樣:

class Console < ActiveRecord::Base 
    belongs_to :brand 

    def full_name 
    "#{brand.name} #{name}" 
    end 
end 

但也許你應該有一些護衛,像這樣:

def full_name 
    ("#{brand.name} " if brand.present?) << "#{name}" 
    end 
+0

'brand.name'就足夠了 - 不需要'self'。不知道如何,但它的工作! –

+0

「品牌」是一種訪問方法。暗示「自我」。 –

+0

......或者你可以把它想成Rails Magic。 :-) –

0

你可能避免錯誤與brand_id參數的存在測試:

def full_name 
    if self.brand_id.present? 
    brand = Brand.find(self.brand_id).name 
    "#{brand} #{self.name}" 
    else 
    self.name #or other combination of parameters not using the brand model 
    end 
end 

讓我們知道,如果這可以幫助你。

相關問題