2011-04-01 60 views
0

我有scaffold有2场name:string, active:boolean..将数据插入2和表1个控制器

这样支架创建数据...

def create 
    @question = Question.new(params[:question]) 
    render :json => @question.to_ext_json(:success => @question.save) 
    end 

一切都很好,但我想,当产生问题的一些数据插入到我的另一个表称为标记:

在我的符号表中只有2场:令牌& IS_ACTIVE(布尔)

def create 
    @w = "token" 
    @question = Question.new(params[:question]) 
    @token = Token.new({:token => @w, :is_active=>"1"}) 
    render :json => @question.to_ext_json(:success => @question.save) 
    end 

这种方式不起作用。我怎么能做到这一点?

回答

0

喜欢的东西...

def create 
    @w = "token" 
    @question = Question.new(params[:question]) 

    if success = @question.save 
    @token = Token.new({:token => @w, :is_active=>"1"}) 
    @token.save 
    end 

    render :json => @question.to_ext_json(:success => success) 
end 

这是假设任何验证有关问题的模型来完成。你可以更进一步并使用交易,但在这种情况下似乎不必要的复杂。