#<Result:0x3904b18>的未定义局部变量或方法`params' [英] undefined local variable or method `params' for #<Result:0x3904b18>

查看:28
本文介绍了#<Result:0x3904b18>的未定义局部变量或方法`params'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的模型中定义了一个方法

def generate_resultself.ncbi_ref_seq=params[:ncbi_ref_seq]Bio::NCBI.default_email = "haha@hotmail.com"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.com"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.com"
    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
end

In 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)
  ...
end

and 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.com"
  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
 end

You may want to use something more descriptive than 'params', but this is the general approach.

这篇关于#&lt;Result:0x3904b18&gt;的未定义局部变量或方法`params'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆