यह काम करना चाहिए:
def upload
render :json => { :attachmentPartial => render_to_string('messages/_attachment', :layout => false, :locals => { :message => @message }) }
end
सूचना और आंशिक (क्योंकि render_to_string एक आंशिक, इसलिए उम्मीद नहीं है के नाम से पहले में अंडरस्कोर _
render_to_string: लेआउट => झूठी भी)।
अद्यतन
आप उदाहरण के लिए एक json
अनुरोध अंदर html
प्रस्तुत करना चाहते हैं, मैं सुझाव है कि आप application_helper.rb
में कुछ इस तरह जोड़ें:
# execute a block with a different format (ex: an html partial while in an ajax request)
def with_format(format, &block)
old_formats = formats
self.formats = [format]
block.call
self.formats = old_formats
nil
end
तो फिर तुम सिर्फ यह कर सकते हैं आपकी विधि में:
def upload
with_format :html do
@html_content = render_to_string partial: 'messages/_attachment', :locals => { :message => @message }
end
render :json => { :attachmentPartial => @html_content }
end
हां, आपको इसे पूरा करने में सक्षम होना चाहिए। मुझे लगता है कि वाक्यविन्यास गलत है हालांकि। चेकआउट: http://stackoverflow.com/questions/2088280/in-rails-how-do-you-render-json-using-a- एक वर्किंग वर्जन के लिए देखें। – raidfive
सवाल अलग है :), जेसन में आंशिक, आंशिक –
अच्छा पकड़ में जेसन नहीं :) अनुमान है कि मैंने कभी इस तरह से कोशिश नहीं की है – raidfive