2011-08-18 59 views
2

我有一個簡單的模型實體,名爲「entry」,其中一個字段爲「content」,保存或更新時我想處理該內容。軌道模型中的成員變量未按預期方式設置

但是在模型的方法中,成員變量@content是零,在控制器函數中它仍然被設置。

entries_controller.rb

def create 
    @entry = Entry.new(params[:entry]) 

    respond_to do |format| 

     if @entry.save 
     #@entry.process 
     @entry.splitwords 

entry.rb

class Entry < ActiveRecord::Base 

    def splitwords 
    logger.debug "content: #{@content.inspect} " 

    words = @content.split(" ") 
    ... 

,並從日誌

entry: #<Entry id: 6, content: "Air-Berlin-Vorstandschef Joachim Hunold legt sein A...", created_at: "2011-08-18 09:30:52", updated_at: "2011-08-18 09:30:52"> 
content: nil 

那麼,爲什麼@content沒有設置的條目裏面那?

回答

2

你的問題是@content,它確實沒有在你的模型中定義。你想要的是如果你想要調用「內容」或「self.content」。所以你的代碼是:

def splitwords 
    logger.debug "content: #{content.inspect} " 

    words = content.split(" ") 
    ...