Turn a partial into a json variable in the controller
Hi I need to turn the html of a partial into a json object (NO开发者_运维问答T TO BE RENDERED), but to be stored in a seperate format.
Something like this:
@json = (:partial => "/answers/likers" ,:type => :html)
Although the above obviously does not work, but hope you get the point, thanks!
Try render_to_string
. It takes the same arguments as render
, and just returns a string instead of outputting the data.
It looks like it's being deprecated but I can't find any new method that provides that same functionality for Rails 3. I tested it on a local Rails 3 setup, though, and it works for me. If anyone knows the "new" way to do this in Rails 3 please let me know, I'm interested now :)
Rails has a .to_json function. However, I never used this together with a partial ...
精彩评论