{"id":1044,"date":"2022-08-30T15:11:27","date_gmt":"2022-08-30T15:11:27","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/local-variable-undefined-in-respond_to-json-block-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:11:27","modified_gmt":"2022-08-30T15:11:27","slug":"local-variable-undefined-in-respond_to-json-block-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/local-variable-undefined-in-respond_to-json-block-collection-of-common-programming-errors\/","title":{"rendered":"Local variable undefined in respond_to json block-Collection of common programming errors"},"content":{"rendered":"<p>I have the following block of code that renders a form. The form will have it&#8217;s &#8220;action&#8221; set depending on the format requested.<\/p>\n<pre><code>respond_to do |format|\n  format.html { render \"new\", :locals =&gt; {:format =&gt; \"html\"} }\n  format.json do\n    render 'new.html', {\n      :locals       =&gt; {:format =&gt; \"json\"},\n      :content_type =&gt; 'text\/html',\n      :layout       =&gt; false\n    } \n  end\nend\n<\/code><\/pre>\n<p>The problem is with <code>:locals =&gt; {:format =&gt; \"json\"}<\/code>. The <code>format.html<\/code> block works fine, but the form rendered by <code>format.json<\/code> has the <code>@format<\/code> variable defined but blank. Where have I gone wrong?<\/p>\n<ol>\n<li>\n<p>Local variables are not set as instance variables when passed to render with the <code>locals<\/code> option. The format variable should be available as just that, a local variable (<code>format vs @format<\/code>)<\/p>\n<p>The reason you&#8217;re seeing @format as being defined is because that is how Ruby works. When instance variables are accessed before they are assigned to a value, they will return nil:<\/p>\n<pre><code>@format # =&gt; nil\n@format = 'value'\n@format # =&gt; 'value'\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:18:23. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have the following block of code that renders a form. The form will have it&#8217;s &#8220;action&#8221; set depending on the format requested. respond_to do |format| format.html { render &#8220;new&#8221;, :locals =&gt; {:format =&gt; &#8220;html&#8221;} } format.json do render &#8216;new.html&#8217;, { :locals =&gt; {:format =&gt; &#8220;json&#8221;}, :content_type =&gt; &#8216;text\/html&#8217;, :layout =&gt; false } end end [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1044","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1044","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1044"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1044\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}