我在我的模型中定义了一个方法
def generate_resultself.ncbi_ref_seq=params[:ncbi_ref_seq]Bio::NCBI.default_email = "haha@hotmail"fasta_sequence= Bio::NCBI::REST::EFetch.nucleotide(@result.ncbi_ref_seq,"fasta")fasta=Bio::FastaFormat.new(fasta_sequence)self.genome_seq = fasta.dataself.genome_sample = fasta.definition结尾在视图中,我使用以下方法传递 ncbi_ref_seq 值:
<%= form_for(@result) do |f|%><% if @result.errors.any?%><div id="error_explanation"><h2><%=pluralize(@result.errors.count, "error") %>禁止保存此结果:</h2><ul><% @result.errors.full_messages.each do |msg|%><li><%=msg%></li><%结束%><%结束%><div class="field"><%= f.label :ncbi_ref_seq %><br><%= f.text_field :ncbi_ref_seq %>
<div类=按钮"><%=f.submit%>
<%结束%>
谁能告诉我为什么会出现错误?
解决方案参数仅在控制器中对您可用.
但是,您可以将参数作为参数从控制器传递给模型.如果您使用名为创建"的控制器操作:
def 创建ClassName.generate_result(params)...结尾您的模型方法可能如下所示:
def generate_result(params)self.ncbi_ref_seq=params[:ncbi_ref_seq]Bio::NCBI.default_email = "spykix@hotmail"fasta_sequence= Bio::NCBI::REST::EFetch.nucleotide(@result.ncbi_ref_seq,"fasta")fasta=Bio::FastaFormat.new(fasta_sequence)self.genome_seq = fasta.dataself.genome_sample = fasta.definition结尾您可能想使用比params"更具描述性的内容,但这是通用方法.
I have a method defined in my model
def generate_result self.ncbi_ref_seq=params[:ncbi_ref_seq] Bio::NCBI.default_email = "haha@hotmail" fasta_sequence= Bio::NCBI::REST::EFetch.nucleotide(@result.ncbi_ref_seq,"fasta") fasta=Bio::FastaFormat.new(fasta_sequence) self.genome_seq = fasta.data self.genome_sample = fasta.definition endIn the view, I pass the ncbi_ref_seq value using the following method:
<%= form_for(@result) do |f| %> <% if @result.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@result.errors.count, "error") %> prohibited this result from being saved:</h2> <ul> <% @result.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :ncbi_ref_seq %><br> <%= f.text_field :ncbi_ref_seq %> </div> <div class = "button"> <%=f.submit%> </div> <% end %>Can someone tell me how come i get the error?
解决方案The params are only available to you in the controller.
However, you can pass the params as a parameter from the controller to the model. If you were using a controller action called 'create':
def create ClassName.generate_result(params) ... endand your model method could look like this:
def generate_result(params) self.ncbi_ref_seq=params[:ncbi_ref_seq] Bio::NCBI.default_email = "spykix@hotmail" fasta_sequence= Bio::NCBI::REST::EFetch.nucleotide(@result.ncbi_ref_seq,"fasta") fasta=Bio::FastaFormat.new(fasta_sequence) self.genome_seq = fasta.data self.genome_sample = fasta.definition endYou may want to use something more descriptive than 'params', but this is the general approach.
更多推荐
#<Result:0x3904b18>的未定义局部变量或方法`params'
发布评论