Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Thanks to samuelkadolph for helping me in the #rubyonrails IRC channel today. He provided a solution in <a href="https://gist.github.com/745821" rel="nofollow">this gist</a>, copied below for convenience:</p> <pre><code>def custom_respond_with(*resources, &amp;block) options = resources.extract_options! if options[:callback] old_block = block block = lambda do |format| old_block.call(format) if block_given? format.json { render :json =&gt; [] } end end respond_with(*(resources &lt;&lt; options), &amp;block) end </code></pre> <p>I haven't tried this in my application yet, but I can see that it should work. He also confirmed that I could similarly override the <code>respond_with</code> method itself simply by changing the name of this method and changing the last line of the definition to <code>super(*(resources &lt;&lt; options), &amp;block)</code>.</p> <p>I think this will work for me. However, I'm still interested in knowing how to write a custom responder to do the job. (It would be a more elegant solution, IMHO.)</p> <p><strong>Update:</strong> I tried this in my application and it works with some minor changes. Here is the version I'm using now in the <code>private</code> section of my ApplicationController, designed to automatically provide the <code>:callback =&gt; params[:callback]</code> option to JSON requests:</p> <pre><code>def custom_respond_with(*resources, &amp;block) options = resources.extract_options! if params[:callback] old_block = block block = lambda do |format| old_block.call(format) if block_given? format.json { render :json =&gt; resources, :callback =&gt; params[:callback] } end end respond_with(*(resources &lt;&lt; options), &amp;block) end </code></pre> <p>Note that I had to change <code>if options[:callback]</code> to <code>if params[:callback]</code> in order to get it working.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload