0

由于这个问题之外的原因,我需要在创建图像时使用 JSON 响应:

<%=
  raw({
    ...
    :url => @image.url(:width => @width, :height => @height),
    :edit_link => render({:state => :image_edit_table_cell, :template_format => :html}, @image),
    # btw, I've also tried passing :format => :html
    :delete_link => link_to_destroy_image(@image, :remote => true, :path => url_for_event(:destroy, :image_id => @image)),
    ...
  }.to_json)
%>

但是,在那个 JSON 中,我想包含一些非常漂亮的 HTML。为了包括这一点,我渲染了一个状态/视图(如您在该部分中所见:edit_link)。这是image_edit_table_cell.html.haml小部件的视图:

= link_to_edit_image @image, :remote => true
# ... doesn't really matter - as long as you know this is html.haml

但是,当我创建图像并呈现 JSON 时,出现Missing template错误:

ActionView::Template::Error (Missing template cell/rails/image_edit_table_cell with {:handlers=>[:builder, :rjs, :rhtml, :erb, :haml, :rxml], :locale=>[:en, :en], :formats=>[:json]} in view paths "/home/ramon/source/unstilted/app/widgets", "/home/ramon/source/unstilted/app/widgets/layouts" and possible paths image/table/image_edit_table_cellapplication/image_edit_table_cellapotomo/widget/image_edit_table_cellcell/rails/image_edit_table_cell):
    6:     :caption => @image.caption || "",
    7:     :text_tag => @image.text_tag(:width => @width, :height => @height),
    8:     :url => @image.url(:width => @width, :height => @height),
    9:     :edit_link => render({:state => :image_edit_table_cell, :template_format => :html}, @image),
    10:     :delete_link => link_to_destroy_image(@image, :remote => true, :path => url_for_event(:destroy, :image_id => @image)),
    11:     :success => true
    12:   }.to_json)
  app/widgets/image/table_widget.rb:37:in `image_edit_table_cell'
  app/widgets/image/table/new_image.json.erb:9:in `_app_widgets_image_table_new_image_json_erb___457172591_99648970_0'
  app/widgets/image/table_widget.rb:16:in `add_image'
  app/widgets/image/uploader_widget.rb:22:in `upload'

似乎问题在于,由于我是从 JSON 文件呈现它,它不知道要查找 HTML 文件(因此它要查找的格式是json. 这就是您看到该:template_file => :html选项的原因(我从这里得到) ) 在上面的第一个代码块中。

我正在使用这些宝石:

  • “轨道”,“3.0.10”
  • '细胞','3.6.2'
  • 'apotomo',“1.1.2”
4

1 回答 1

0

显然,这是 Rails 问题,而不是 Apotomo 问题。这个答案为我做了!关键是在渲染 html 视图之前在视图文件中设置格式:

<% self.formats = ["html"] %>
于 2011-08-25T14:16:47.740 回答