模型+控制器继承

时间:2013-07-10 11:53:48

标签: ruby-on-rails ruby-on-rails-3 inheritance ruby-on-rails-4

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

我可以知道如何从生成器获取数据,以便我可以获得Generator的值并为结果执行必要的操作吗?

#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

在我的generatorController中

#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
ApplicationController中的

#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

1 个答案:

答案 0 :(得分:1)

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

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

  2. 如果您想在ResultsController中使用Generator实例,则需要一种方法来检索它。来自params或来自Result实例的外键。

  3. 请勿使用after_action修改模型,一切都可能发生,模型可能尚未保存,应用流程也非常模糊。

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

    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