如何从模型函数中获取值

时间:2013-07-10 13:57:28

标签: ruby-on-rails

嗨我从下面的代码中找回一个空值。我不知道出了什么问题。 some1能帮助我吗?

class GeneratorsController < ApplicationController
  before_action :set_generator, only: [:show, :edit, :update, :destroy]
  # GET /generators
  # GET /generators.json
  def index
    @generators = Generator.all
  end

  # GET /generators/1
  # GET /generators/1.json
  def show
  end

  # GET /generators/new
  def new
    @generator = Generator.new
  end

  # GET /generators/1/edit
  def edit
  end

  # POST /generators
  # POST /generators.json
  def create    
    @generator = Generator.new(generator_params)

    @generator.choice = params[:choice]
    if params[:choice] == 'Randomly'
        @generator.random_generate(params[:primer_length],params[:random_primer_generated],params[:c_primer])
    #elsif params[:choice] == 'No_of_ATGC'
    #    @generator.no_ATGC
    #elsif params[:choice] == 'Seating'
    #    @generator.seating
    end

    respond_to do |format|
      if @generator.save
        format.html { redirect_to @generator, notice: 'Generator was successfully created.' }
        format.json { render action: 'show', status: :created, location: @generator }
      else
        format.html { render action: 'new' }
        format.json { render json: @generator.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /generators/1
  # PATCH/PUT /generators/1.json
  def update
    respond_to do |format|
      if @generator.update(generator_params)
        format.html { redirect_to @generator, notice: 'Generator was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @generator.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /generators/1
  # DELETE /generators/1.json
  def destroy
    @generator.destroy
    respond_to do |format|
      format.html { redirect_to generators_url }
      format.json { head :no_content }
    end
  end

  private
    def set_generator
      @generator = Generator.find(params[:id])
    end

    def generator_params
      params.require(:generator).permit(:primer_length, :choice, :random_primer_generated)
    end
end

在我的模特中

class Generator < ActiveRecord::Base
      attr_accessible :choice, :primer_length, :random_primer_generated, :genome_sample
      belongs_to :result

      def random_generate(primer_length,random_primer_generated,c_primer)
         length = primer_length
         length=length.to_i
            chars = 'ATGC'
            seq = ''
            length.times { seq << chars[rand(chars.size)] }
           self.random_primer_generated = seq
           c_primer =Bio::Sequence::NA.new(self.random_primer_generated.reverse)
           self.c_primer=c_primer.complement.upcase
      end 
end

没有错误但我得到了random_primer_generated和c_primer属性的空值

2 个答案:

答案 0 :(得分:1)

我打赌你在调用random_generate后忘记将Generator实例保存到数据库。

如果这不是问题,您是否可以在Rails控制台中发布一段交互,显示您如何设置这两个属性以及它们最终如何成为零?很难从提供的信息中准确地告诉您如何重现问题。控制台会话可以清除内容,并允许您与控制器分开测试模型。

答案 1 :(得分:1)

您应该在创建操作中使用generator_params而不是params

这超出了问题的范围,但重构的生成器可能是:

  def random_generate(primer_length)
     chars = 'ATGC'
     seq = ''
     primer_length.to_i.times { seq << chars[rand(chars.size)] }
     self.random_primer_generated = seq
     c_primer =Bio::Sequence::NA.new(self.random_primer_generated.reverse)
     self.c_primer=c_primer.complement.upcase
  end

你应该尝试它实际上在这样的rails console中工作,例如:

g = Generator.new
g.random_generate(4)

单元测试Generator模型甚至更好,只需使用rails控制台;)