rails模型中的成员变量未按预期设置

时间:2011-08-18 09:34:07

标签: ruby-on-rails ruby model

我有一个简单的模型实体,名为“entry”,其中包含一个字段“content”,在保存或更新时,我想处理该内容。

但是在模型的方法中,成员变量@content是nil,在控制器函数中它仍然是设置的。

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没有在条目中设置?

1 个答案:

答案 0 :(得分:2)

您的问题是@content,确实没有在您的模型中定义。如果你愿意,你想要的是调用“内容”或“自我内容”。所以你的代码是:

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

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