Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-3.1

DoubleRenderError error and would like to return a partial as a string to be sent back in json


I'd like to do something like the following:

def return_item
  item_id=params[:item_id]
  @item=Item.find(item_id)
  str=render :partial => 'headers/item'

  r={}
  r[:status]='success'
  r[:data]=str
  render :json => r.to_json
end

but I am getting a DoubleRender error (which IMHO, I'm not doing). How would / could I get the values from a render and save it to a string that could then be rendered out as part of a JSON response?

thx in advance


Solution

  • render_to_string is probably what you want.

    Try:

    str=render_to_string :partial => 'headers/item'
    

    Take a look at http://guides.rubyonrails.org/layouts_and_rendering.html#using-render for more info.