我有 2 个表,生成器和结果。
生成器 has_many results result belongs_to 生成器
我怎样才能有一个页面来显示生成器的所有值和结果?在我的结果控制器创建中,我必须链接 Generator_id 吗?我已经尝试过,但是当我尝试创建属于该生成器的新结果时,它会给我一个错误。
例如 localhost:3000/generator/3/results/new ,单击 create 后,它给了我以下错误:
Couldn't find Generator without an ID .
我收到了一些建议,要求我创建一个新的控制器来这样做。如果是这样,我应该如何将我的 @generator 和 @result 传递给它?
发电机控制器
def index
@generators = Generator.all(:include => [:results])
end
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
结果模型
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
结果控制器
def index
@results = Result.all(:include => :generator)
end
def create
@result = Result.new
@result = @result.generate_result(result_params)
@generator = Generator.find(params[:id])
@result = generator.results.build(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