变量变为空

时间:2011-08-05 19:40:49

标签: ruby-on-rails model null controller

我有一个变量以某种方式变为null!请注意控制器中的调试器。 编辑:看起来cvit对象在控制器和视图中是不一样的。我该如何解决这个问题?

在视图中:

<%= content_tag(:img,
      content_tag(:map, @cvit.overlay_coords_gray.html_safe, :name => "#CViT_image_gray"),
        {:src => "/data/04_cvit/#{@cvit.cvt_file}_gray.png", :usemap =>"#CViT_image_gray", :style => "display:block;margin-left:auto;margin-right:auto"} )
%>

我收到此错误:

undefined method `html_safe' for nil:NilClass

指的是@ cvit.overlay_coords_gray

这是基本模型和控制器:

模型:

class Cvit < ActiveRecord::Base
  attr_accessible :species,:program,:textup,:e_value,:filter,:min_identity,:cluster_dist,:fileup_file_name
  attr_accessor :base_path, :fa_file, :text_file, :dbase, :source, :bl_file, :bl_sorted, :gff_file, :cvt_file, :db, :overlay_coords_gray

  def initilize(*args)
     super(*args)
  end

  def cvitSetup()
    self.base_path = "blast_cvit/"
    self.fa_file = "input.fa"
    .
    .
  end
end

控制器:

def show
    @cvit = Cvit.find(params[:id])
    @cvit.cvitSetup()
    @cvit.blast()
    @cvit.generateGff()
    @cvit.generateCvitImage()

    logger.debug "overlay coords: #{@cvit.overlay_coords_gray.inspect}"


    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @cvit }
    end
  end

日志信息(通知使用调试器显示其非空!!!!)

Creating scope :page. Overwriting existing method Cvit.page.
  SQL (0.5ms)  SHOW TABLES
  Cvit Load (0.3ms)  SELECT `cvits`.* FROM `cvits` WHERE `cvits`.`id` = 5 LIMIT 1


Started GET "/cvits/5" for 129.186.136.212 at Fri Aug 05 11:19:50 -0500 2011
  Processing by CvitsController#show as HTML
  Parameters: {"id"=>"5"}
Creating scope :page. Overwriting existing method Cvit.page.
      !!!DEBUGGER!!! overlay coords: "<area onmouseover=\"mouseover_toggle('Glyma02g37330..173..179..694..700')\" shape=\"rect\" coords=\"173,694,179,700\" href=\"http://soybase.org/gb2/gbrowse/gmax1.01/?start=42605601;stop=42707190;ref=Gm02;h_feat=Glyma02g37330@yellow\" target=\"_blank\"></area>\n<div id=\"Glyma02g37330..173..179..694..700\" style=\"border: 2px solid rgb(0, 0, 0);background-color: rgb(239,237,233); z-index: 1; visibility: hidden; position: absolute; width: 250px; left: 394px; top: 774px;\"><input type=\"button\" name=\"closeDiv\" value=\"Close\" onclick=\"mouseover_toggle('Glyma02g37330..173..179..694..700')\"></input><br /> Query = Glyma02g37330 <br />Target = Glyma02g37330 <br />Chromosome = Gm02 <br />Start = 42605601 <br />End = 42707190  <br /><a href=\"http://soybase.org/gb2/gbrowse/gmax1.01/?start=42605601;stop=42707190;ref=Gm02;h_feat=Glyma02g37330@yellow\">Click to view in GBrowse</a></div>\n<area onmouseover=\"mouseover_toggle('Glyma02g37330..1013..1019..723..729')\" shape=\"rect\" coords=\"1013,723,1019,729\" href=\"http://soybase.org/gb2/gbrowse/gmax1.01/?start=44520215;stop=44621936;ref=Gm14;h_feat=Glyma14g35580@yellow\" target=\"_blank\"></area>\n<div id=\"Glyma02g37330..1013..1019..723..729\" style=\"border: 2px solid rgb(0, 0, 0);background-color: rgb(239,237,233); z-index: 1; visibility: hidden; position: absolute; width: 250px; left: 394px; top: 774px;\"><input type=\"button\" name=\"closeDiv\" value=\"Close\" onclick=\"mouseover_toggle('Glyma02g37330..1013..1019..723..729')\"></input><br /> Query = Glyma02g37330 <br />Target = Glyma14g35580 <br />Chromosome = Gm14 <br />Start = 44520215 <br />End = 44621936  <br /><a href=\"http://soybase.org/gb2/gbrowse/gmax1.01/?start=44520215;stop=44621936;ref=Gm14;h_feat=Glyma14g35580@yellow\">Click to view in GBrowse</a></div>\n<area onmouseover=\"mouseover_toggle('Glyma02g37330..313..319..147..153')\" shape=\"rect\" coords=\"313,147,319,153\" href=\"http://soybase.org/gb2/gbrowse/gmax1.01/?start=6118288;stop=6218620;ref=Gm04;h_feat=Glyma04g07910@yellow\" target=\"_blank\"></area>\n<div id=\"Glyma02g37330..313..319..147..153\" style=\"border: 2px solid rgb(0, 0, 0);background-color: rgb(239,237,233); z-index: 1; visibility: hidden; position: absolute; width: 250px; left: 394px; top: 774px;\"><input type=\"button\" name=\"closeDiv\" value=\"Close\" onclick=\"mouseover_toggle('Glyma02g37330..313..319..147..153')\"></input><br /> Query = Glyma02g37330 <br />Target = Glyma04g07910 <br />Chromosome = Gm04 <br />Start = 6118288 <br />End = 6218620  <br /><a href=\"http://soybase.org/gb2/gbrowse/gmax1.01/?start=6118288;stop=6218620;ref=Gm04;h_feat=Glyma04g07910@yellow\">Click to view in GBrowse</a></div>\n"
  CACHE (0.0ms)  SELECT `cvits`.* FROM `cvits` WHERE `cvits`.`id` = 5 LIMIT 1
Rendered cvits/show.html.erb within layouts/glycine_max (3.3ms)
Completed 500 Internal Server Error in 4942ms

ActionView::Template::Error (undefined method `html_safe' for nil:NilClass):


    88:         <a href="/data/02_blastout/<%= @cvit.bl_file %>"><%= bl_state %></a>, 
    89:     <a href="/data/03_gff/<%= @cvit.gff_file %>">CViT gff file</a>
    90:     <%= content_tag(:img, 
    91:             content_tag(:map, @cvit.overlay_coords_gray.html_safe, :name => "#CViT_image_gray"), 
    92:         {:src => "/data/04_cvit/#{@cvit.cvt_file}_gray.png", :usemap =>"#CViT_image_gray", :style => "display:block;margin-left:auto;margin-right:auto"} ) 
    93:             
    94:     %>
  app/views/cvits/show.html.erb:91:in `_app_views_cvits_show_html_erb___1813186540_17224422620_0'
  app/controllers/cvits_controller.rb:38:in `show'

Rendered /usr/local/lib/ruby/gems/1.8/gems/actionpack-3.0.8/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.3ms)
Rendered /usr/local/lib/ruby/gems/1.8/gems/actionpack-3.0.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (8.8ms)
Rendered /usr/local/lib/ruby/gems/1.8/gems/actionpack-3.0.8/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (16.5ms)

2 个答案:

答案 0 :(得分:0)

您在视图中的某处启用了片段缓存:

CACHE (0.0ms)  SELECT `cvits`.* FROM `cvits` WHERE `cvits`.`id` = 10 LIMIT 1

你得到了错误的对象:你要求'5'并且看到上面的查询是10。

禁用缓存并再试一次,无论如何都应修复此缓存。

答案 1 :(得分:0)

在渲染之前尝试在控制器中执行@dummy = @ cvit.clone或@dummy = @ cvit.dup;然后在视图中使用@dummy而不是@cvit。此外,执行此操作后,如果attr_accessors的值已复制到@dummy,请检入控制器本身。如果他们被复制了,那么查看视图并查看attr_accessors现在是否为非零? - Zabba 2011年8月5日21:37 哇谢谢你Zabba !!!!!!虽然克隆和重复有不同的object_ids,但他们并没有把attr_accessor变量弄掉!谢谢谢谢!!!!!!!!!!!! - 本杰明2011年8月5日21:49