<?xml version="1.0" encoding="UTF-8"?>
<post>
  <body>&lt;p&gt;Um pequeno tutorial em portugu&amp;ecirc;s pra diferenciar melhor &lt;em&gt;respond_to?&lt;/em&gt; e &lt;em&gt;respond_to&lt;/em&gt;. N&amp;atilde;o parece ter muita n&amp;atilde;o? Mas o tio Chuck Norris vai me ajudar a esclarecer melhor isso aqui.&lt;/p&gt; &lt;p&gt;&lt;a href=&quot;http://ruby-doc.org/core/classes/Object.html#M000333&quot;&gt;respond_to?&lt;/a&gt; &amp;eacute; um m&amp;eacute;todo do Ruby Core como voc&amp;ecirc; pode ver, e n&amp;atilde;o do Ruby On Rails, ele basicamente verifica se determinado objeto responde a um m&amp;eacute;todo, como o nome sugere mesmo.&lt;/p&gt; &lt;p&gt;Por exemplo:&lt;/p&gt; 
&lt;/pre&gt;&lt;pre&gt;&lt;span class=&quot;keyword&quot;&gt;class &lt;/span&gt;&lt;span class=&quot;class&quot;&gt;Chuck&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;Object&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;norris&lt;/span&gt;
    &lt;span class=&quot;ident&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;the man&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;ident&quot;&gt;chuck_norris&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;Chuck&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;new&lt;/span&gt;
&lt;span class=&quot;ident&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;chuck_norris&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;respond_to?&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;norris&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;)&lt;/span&gt; &lt;span class=&quot;comment&quot;&gt;# true&lt;/span&gt;
&lt;span class=&quot;ident&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;chuck_norris&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;respond_to?&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;mr_t&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;)&lt;/span&gt; &lt;span class=&quot;comment&quot;&gt;# false&lt;/span&gt;
&lt;/pre&gt;

 &lt;p&gt;J&amp;aacute; o &lt;a href=&quot;http://api.rubyonrails.org/classes/ActionController/MimeResponds/InstanceMethods.html#M000206&quot;&gt;respond_to&lt;/a&gt;, sem o ponto de interroga&amp;ccedil;&amp;atilde;o (eles fazem parte do nome do m&amp;eacute;todo) faz parte do ActionController do Ruby On Rails. Ele coloca suporte a Web Service na sua aplica&amp;ccedil;&amp;atilde;o, isso quer dizer que ele consegue resolver o tipo do recurso que voc&amp;ecirc; deseja.&lt;/p&gt; 

&lt;pre&gt;&lt;span class=&quot;ident&quot;&gt;respond_to&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt;
  &lt;span class=&quot;ident&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;html&lt;/span&gt; &lt;span class=&quot;comment&quot;&gt;# render por default nome_da_acao.html.erb&lt;/span&gt;
  &lt;span class=&quot;ident&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;js&lt;/span&gt; &lt;span class=&quot;comment&quot;&gt;# render o nome_da_acao.rjs&lt;/span&gt;
  &lt;span class=&quot;ident&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;xml&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;render&lt;/span&gt; &lt;span class=&quot;symbol&quot;&gt;:xml&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;attribute&quot;&gt;@chuck&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;to_xml&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;comment&quot;&gt;# coloca o chuck pra xml e envia&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;Com esse c&amp;oacute;digo na action voc&amp;ecirc; poderia usar, por exemplo:&lt;/p&gt; &lt;pre&gt;&lt;code&gt;http://localhost:3000/nome_da_acao.html&lt;code&gt;&lt;/code&gt;&lt;/code&gt;&lt;/pre&gt; &lt;pre&gt;&lt;code&gt;http://localhost:3000/nome_da_acao.xml&lt;code&gt;&lt;/code&gt;&lt;/code&gt;&lt;/pre&gt; &lt;p&gt;Ou realizar uma chamada JS unobstrusive usando AJAX.&lt;/p&gt; &lt;p&gt;Isso ai, espero ter ajudado.&lt;/p&gt;</body>
  <created-at type="datetime">2008-10-21T13:18:30Z</created-at>
  <id type="integer">34</id>
  <permalink>diff-entre-respond_to-e-respond_to</permalink>
  <title>Diff entre respond_to? e respond_to</title>
  <updated-at type="datetime">2008-11-13T19:49:58Z</updated-at>
  <user-id type="integer">1</user-id>
</post>
