Search code examples
jsonrubysinatralocals

Sinatra - Locals not displaying in erb


I have JSON that I want to display in a tag on my submitted.erb but I can't get it to display. When I PUT the JSON, I see the entirety of the parsed JSON in the console.

Here's the flow:

  1. User submits a form from myform.erb
  2. In my post, the following is done with the JSON:

put '/' do require json ... @resp = JSON.pretty_generate(JSON.parse(json)) redirect "/submitted/" end

get '/submitted/?' do erb :submitted, :locals => {:results => @resp} end

Then in my submitted.erb I have <%= results %> in my pre tags.


Solution

  • If you want to output the string version of your object in erb you can do

    <%= results.inspect %>
    

    But you probably just need it as a string so try change

    @resp = JSON.pretty_generate(JSON.parse(json))
    

    to

    @resp = JSON.parse(json).to_s