开发者

Storing the output of a partial to a local var, then send as json to ajax request

开发者 https://www.devze.com 2023-03-07 04:12 出处:网络
I have a ajax call, and I want to return a json response. The controller will need to get the output of a partial, plus add some other properties to the object then serialize to json.

I have a ajax call, and I want to return a json response.

The controller will need to get the output of a partial, plus add some other properties to the object then serialize to json.

How can I get the output of a partial and store it to this object?

I want something like:

def my_action

   my_output.html = render :partial => 'test', ....
   my_output.some_prop1 = 234234

   my_output.to_json
end

Then in my view I will inject the htm开发者_如何学Gol into the DOM, etc.


Just use render_to_string in place of render.

def my_action
   output = {}
   output[:html] = render_to_string :partial => 'test', ....
   output[:some_prop1] = 234234
   render :json => output
end
0

精彩评论

暂无评论...
验证码 换一张
取 消