0

我正在尝试获取所有生成器的结果,并将结果合并到 1 个表中。我有以下关联:

生成器 has_many: 结果结果 belongs_to:generators

每当我想创建一个新结果时,我都会收到一条错误消息,说找不到没有 ID 的生成器。为什么我会收到错误消息?我应该如何解决?

例如:localhost:3000/generators/new = 是我在生成器表单中输入值的部分,单击创建按钮后,它会将我带到 localhost:3000/generators/8/results/new。在此表单中键入值后,我收到错误消息,说找不到没有 ID 的生成器。我正在尝试创建一个页面,我可以在其中同时显示 Generators + Result 值。在 1 页中显示所有生成器及其各自的结果数据。

发电机控制器

class GeneratorsController < ApplicationController
  before_action :set_generator, only: [:show, :edit, :update, :destroy]
  # GET /generators
  # GET /generators.json
  def index
    @generators = Generator.all(:include => [:results])
  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(generator_params)
      elsif params[:choice] == 'No_of_ATGC'
          @generator.no_ATGC(params[:no_A],params[:no_T],params[:no_G],params[:no_C])
      elsif params[:choice] == 'Seating'
          @generator.seating(params[:user_seq])
      end

    @generator.result_choice=params[:result_choice]
    respond_to do |format|
      if @generator.save
          if @generator.result_choice == 'Yes'
            format.html { redirect_to(new_generator_result_path(@generator)) }
          else
            format.html { redirect_to @generator, notice: 'Result was successfully created.' }
            format.json { render action: 'show', status: :created, location: @generator }
          end
      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,:no_A,:no_T,:no_G,:no_C,:user_seq)
    end
end

结果.rb

class Result < ActiveRecord::Base
   attr_accessible :ncbi_ref_seq,:genome_seq, :genome_sample
   belongs_to :generator, foreign_key: "generator_id"

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

    g=Generator.last
    p=self.genome_seq.scan(g.c_primer)
    self.binding_times= p.length()      
  end

end

ResultController [错误在 def create 行突出显示;生成器 = Generator.find(params[:id]) ]

class ResultsController < ApplicationController
  before_action :set_result, only: [:show, :edit, :update, :destroy]
  # 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 = @result.generate_result(result_params)
    generator = Generator.find(params[:id]) 
    @result = generator.results.build(result_params)

   # generator = Generator.find(3)
  #  @result = generator.results.build(result_params) 
   # @result=@result.generate_result(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

  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

生成器/index.html.erb

<h1>[Index]Random Amplified Polymorphic DNA [RAPD] Primer Generator</h1>
<table>
  <thead>
    <tr>
      <th>Primer length</th>
      <th>Selected choice</th>
      <th>Random primer generated</th>
      <th>Complimentary primer</th>
      <th>NCBI ref seq</th>
      <th>Genome sample</th>
      <th>Binding times</th>
    </tr>
  </thead>

  <tbody>
     <% @generators.each do |generator| %>
     <tr>
        <td><%= generator.primer_length %></td>
        <td><%= generator.choice %></td>
        <td><%= generator.random_primer_generated %></td>
        <td><%= generator.c_primer %></td>

        <% for result in generator.results %>
        <td><%= result.ncbi_ref_seq %></td>
        <td><%= result.genome_sample %></td>
        <td><%= result.binding_times %></td>
        <%end%> 

        <td><%= link_to 'Show', generator %></td>
        <td><%= link_to 'Edit', edit_generator_path(generator) %></td>
        <td><%= link_to 'Destroy', generator, method: :delete, data: { confirm: 'Are you sure?' } %></td>
    </tr>
     <% end %>
  </tbody>
</table>

<br>

<%= link_to 'Home', root_path %>
<%= link_to 'New Generator', new_generator_path %>
4

1 回答 1

0

如果生成器有很多结果,那么首先你应该在新方法中找到生成器,因为当你创建任何记录时,它会从新方法初始化。我正在重写你的新方法和创建方法,你可以从中参考:

def new
 generator = Generator.find(params[:id])
 @result = generator.results.build(result_params)     
end

# POST /results
# POST /results.json
def create    
  @result = @result.generate_result(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

希望这可能会有所帮助。

于 2013-07-15T15:25:01.700 回答