undefined local variable or method `params' for #<Result:0x3904b18>

a 夏天 提交于 2019-12-24 20:41:41

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/17531705/undefined-local-variable-or-method-params-for-result0x3904b18

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!