title: Ruby form using ajax with remote: true gives ActionController::InvalidAuthenticityToken error. Classic submission does not
body: <p>I am writing a chat page for a RoR site. I have it all worked out in HTML and I am trying to implement it with ajax. There is a form for submission of messages. the form tag reads <code>&lt;%= form_for(@chat, remote: true, :authenticity_token =&gt; true) do |f| %&gt;</code>   </p>
  
  <p>My entire view:</p>
  
  <pre><code>  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Battleriskopoloy - Communications&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;br&gt;
          &lt;div id="heading_1" align="center"&gt;
              &lt;br&gt;&lt;span id="title"&gt;&lt;strong&gt;[ . . . Communications . . . ]&lt;/strong&gt;&lt;/span&gt;
          &lt;/div&gt;&lt;br&gt;
          &lt;div id="sidebar"&gt;&lt;br&gt;
              &lt;% $chat_users.each do |user| %&gt;
              &lt;% user = User.find_by_username(user) %&gt;
              &lt;%= button_to user.username, flop_chat_path(user), :class =&gt; "select", :method =&gt; :get  %&gt;
              &lt;% end %&gt;
          &lt;/div&gt;
          &lt;div id="display"&gt;
              &lt;% $messeges.each do |i| %&gt;
                  &lt;% if i[1] == "from" %&gt;
                  &lt;p style="color:#000000; text-align:right; margin-right:5%;font-family:courier"&gt;&lt;%= i[0] %&gt;&lt;/p&gt;
                  &lt;br&gt;
                  &lt;% else %&gt;
                  &lt;p style="color:#FF0000; text-align:left; margin-left:5%; font-family:courier"&gt;&lt;%= i[0] %&gt;&lt;/p&gt;
                  &lt;br&gt;
                  &lt;%end%&gt;
              &lt;%end%&gt;
          &lt;/div&gt;
          &lt;div id="textfield"&gt;
              &lt;%= form_for(@chat, remote: true, :authenticity_token =&gt; true) do |f| %&gt;
              &lt;%= f.text_field :content, id: "compose" %&gt;
              &lt;br&gt;
              &lt;br&gt;
              &lt;%= f.submit "Send Messege", id: "submit" %&gt;
          &lt;% end %&gt;
          &lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The controller :</p>
  
  <pre><code>class ChatsController &lt; ApplicationController
       $messeges = Array.new(10, " ")
  def new
      $messeges = Array.new
      if $ruser != nil
      $messeges = Array.new
      Chat.all.each_with_index do |i, index|
          if i.recipient == current_user.id &amp;&amp; i.sender == $ruser.id
              $messeges.push([i.content, "to"])
          end
          if i.recipient == $ruser.id &amp;&amp; i.sender == current_user.id
              $messeges.push([i.content, "from"])
          end
      end
      end
      $chat_users = Array.new
      User.all.each do |user|
          if user != nil &amp;&amp; current_user != nil
          if user.game_id == current_user.game_id &amp;&amp; user.id != current_user.id
              $chat_users.push(user.username)
          end
          end
      end
      @chat = Chat.new
  end
  
  def create
      if $ruser != nil
      @chat = Chat.new(chat_params)
      @chat.recipient = $ruser.id
      @chat.sender = current_user.id
      @chat.save
      end
      redirect_to "/comms/new"
  end
  
  def flop
      $ruser = User.find(params[:id])
      $messeges = Array.new
      Chat.all.each_with_index do |i, index|
          if i.recipient == current_user.id &amp;&amp; i.sender == $ruser.id
              $messeges.push([i.content, "to"])
          end
          if i.recipient == $ruser.id &amp;&amp; i.sender == current_user.id
              $messeges.push([i.content, "from"])
          end
      end
      redirect_to "/comms/new"
  end
  
  private
      def chat_params
          params.require(:chat).permit(:content)
      end
  end
  </code></pre>
  
  <p>Additionally, I have <code>&lt;%= csrf_meta_tags %&gt;</code> in my application.html.erb</p>
  
  <p>I had found some other things online about people getting the same error with rails 4 when switching from ajax to html submission but that seemed only to be because they didn't have the <code>:authenticity_token =&gt; true</code> parameter included. Any help is greatly appreciated.</p>