-1

大家好,我有 2 个控制器。生成器和结果。为了获得结果,我需要生成器的值(不知何故,该值已存储在模型中)。

我可以知道我应该如何从生成器获取数据,以便我可以拥有 Generator 的值并为 Result 做我必须做的事情吗?

#result_controller.rb 
class ResultsController < ApplicationController
  before_action :set_result, only: [:show, :edit, :update, :destroy]
  after_action :generate_result, only: [:create]
  # GET /results
  # GET /results.json
  def index
    @results = Result.all
  end

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

  # GET /results/new
  def new
    @result = Result.new
  end

  # GET /results/1/edit
  def edit
  end

  # POST /results
  # POST /results.json
  def create
    @result = Result.new(result_params)
    respond_to do |format|
      if @result.save
        format.html { redirect_to @result, notice: 'Result was successfully created.' }
        format.json { render action: 'show', status: :created, location: @result }
      else
        format.html { render action: 'new' }
        format.json { render json: @result.errors, status: :unprocessable_entity }
      end
    end
  end

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

  # DELETE /results/1
  # DELETE /results/1.json
  def destroy
    @result.destroy
    respond_to do |format|
      format.html { redirect_to results_url }
      format.json { head :no_content }
    end
  end

  def set_ncbi_ref_seq
    @result.ncbi_ref_seq = params[:ncbi_ref_seq]
    generate_result
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_result
      @result = Result.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def result_params
      params.require(:result).permit(:ncbi_ref_seq)
    end
end

在我的生成器控制器中

#generator_controller.rb
class GeneratorsController < ApplicationController
  before_action :set_generator, only: [:show, :edit, :update, :destroy]
  after_action :generate_option, only: [:create, :update]
  # 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)
    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

  def generate_option
    @generator.choice = params[:choice]
    if params[:choice] == 'Randomly'
        random_generate
    elsif params[:choice] == 'No_of_ATGC'
        no_ATGC
    elsif params[:choice] == 'Seating'
        seating
    end
   @generator.save!
  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

在应用程序控制器中

#application_controller.rb
class ApplicationController < ActionController::Base

  protect_from_forgery with: :exception 

  def random_generate
     length = @generator.primer_length
     length=length.to_i
        chars = 'ATGC'
        seq = ''
        length.times { seq << chars[rand(chars.size)] }
       @generator.random_primer_generated = seq

       @generator.c_primer =Bio::Sequence::NA.new(@generator.random_primer_generated.reverse)
       @generator.c_primer=@generator.c_primer.complement.upcase
  end

  def no_ATGC 
    counts = {'A'=>params[:no_A],'T'=>params[:no_T],'G'=>params[:no_G],'C'=>params[:no_C]}
   # total=params[:no_A]+params[:no_T]+params[:no_G]+params[:no_C]
    @generator.random_primer_generated = Bio::Sequence::NA.randomize(counts).upcase
    @generator.primer_length = @generator.random_primer_generated.length

    @generator.c_primer =Bio::Sequence::NA.new(@generator.random_primer_generated.reverse)
    @generator.c_primer=@generator.c_primer.complement.upcase
  end

  def seating()       
    desired_random_primer=params[:user_seq]
    @generator.primer_length = desired_random_primer.length
    user_seq_xspace= params[:user_seq].delete('*') 
    empty_slots=@generator.primer_length-user_seq_xspace.length() 
    length = empty_slots
        chars = 'ATGC'
        seq = ''
        length.times { seq << chars[rand(chars.size)] }
    temp_random_primer_generated = seq

    no=0 
    n=0
    while n < @generator.primer_length do
     if desired_random_primer[n] == "*" 
       desired_random_primer[n]=desired_random_primer[n].replace(temp_random_primer_generated[no])
       no+=1
     else
       desired_random_primer[n]=desired_random_primer[n]
     end
     n+=1
    end
    @generator.random_primer_generated = desired_random_primer 

    @generator.c_primer =Bio::Sequence::NA.new(@generator.random_primer_generated.reverse)
    @generator.c_primer=@generator.c_primer.complement.upcase
  end

  *def generate_result
    ref_seq = @result.ncbi_ref_seq
    Bio::NCBI.default_email = "haha@hotmail.com"
    fasta_sequence = Bio::NCBI::REST::EFetch.nucleotide(ref_seq,"fasta")
    fasta=Bio::FastaFormat.new(fasta_sequence)
    @result.genome_seq = fasta.data
    @result.genome_sample = fasta.definition

    p=@generator.genome_seq.scan(@generator.c_primer)
    @result.binding_times = p.length()

    @result.save
  end*

end
4

1 回答 1

1

您的控制器存在许多问题。

  1. ApplicationController 中的方法用于共享加载或实用程序方法(典型的候选方法:current_user(memoized)将可用于所有控制器。属于特定控制器的方法不会进入 AppController。

  2. 如果您想在 ResultsController 中创建一个 Generator 实例,您需要一种方法来检索它。来自参数或来自 Result 实例上的外键。

  3. 不要after_action用来修改你的模型,一切都有可能发生,模型可能还没有保存,应用流程很模糊。

例如,我宁愿在模型中加入一些智能

class Generator < ActiveRecord::Base

  # other stuff you already have

  def set_primer_from_counts(num_a, num_t, num_g, num_c)
    counts = {'A'=>num_a,'T'=>num_t,'G'=>num_g,'C'=>num_c}
    random_primer_generated = Bio::Sequence::NA.randomize(counts).upcase
    primer_length = random_primer_generated.length
    c_primer = Bio::Sequence::NA.new(random_primer_generated.reverse)
    @generator.c_primer = c_primer.complement.upcase
  end

end


class GeneratorController < ApplicationController

  def create
    @generator = Generator.new(generator_params)
    @generator.set_primer_from_counts(params[:no_A], params[:no_T], params[:no_G], params[:no_C])
    # ....
  end
end
于 2013-07-10T14:21:09.757 回答