2011-08-18 58 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(" ") 
    ...