Apotomo: How do you render a state/view that's in HTML format from another view that's in another format?
For reasons beyond this problem, I need to respond with JSON when creating an image:
<%=
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)
%>
However, in that JSON, I want to include some pretty fancy HTML. To include that, I render a state/view (as you can see in the :edit_link
portion). This is the image_edit_table_cell.html.haml
view of the widget:
= link_to_edit_image @image, :remote => true
# ... doesn't really matter - as long as you know this is html.haml
However, when I create an image, and the JSON is rendered, I get a Missing template
error:
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开发者_如何学C/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'
It seems like the problem is that since I'm rendering it from a JSON file, it doesn't know to look for an HTML file (thus the format it's looking for is json
. That's why you see the :template_file => :html
option (I got that from here) in the first block of code above.
I'm using these gems:
- 'rails', '3.0.10'
- 'cells', '3.6.2'
- 'apotomo', "1.1.2"
Apparently, this is a Rails issue, not an Apotomo issue. This answer did it for me! Key is setting the format in your view file before you render your html view:
<% self.formats = ["html"] %>
精彩评论