-1

嗨,我从下面的代码中取回了一个空值。我不知道出了什么问题。有人可以帮我吗?

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 属性的空值

4

2 回答 2

1

我敢打赌,您在调用后忘记将生成器实例保存到数据库中random_generate

如果这不是问题,您能否在 Rails 控制台中发布一段交互片段,展示您如何尝试设置这两个属性以及它们如何最终变为 nil?从提供的信息中很难准确判断您是如何重现问题的。控制台会话可以解决问题,它允许您独立于控制器测试您的模型。

于 2013-07-10T15:29:53.360 回答
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)

对生成器模型进行单元测试比使用 rails 控制台更好;)

于 2013-07-10T15:45:11.750 回答