title: Rails form_with (remote: true) errors
body: <p>I need some help here,</p>
  
  <p>I'm getting a error when I try to update a model using ajax from Rails (form_with / remote: true). I was able to work fine with XHR requests for urls that were resources by Rails (see routes below), but with custom urls I'm getting a error.</p>
  
  <h3>controller:</h3>
  
  <pre><code>def criar
    @user = current_user
    respond_to do |format|
      if @user.update_attributes(user_params)
        format.js {
          flash[:success] = "Success!"
          redirect_to root_path
        }
      else
        format.js 
      end
    end
  end
  </code></pre>
  
  <h3>rspec (request):</h3>
  
  <pre><code>put user_criar_path(user), xhr: true,
   :params =&gt; { ... }
  </code></pre>
  
  <h3>view:</h3>
  
  <pre><code>&lt;%= form_with model: @user, url: user_criar_path(@user), method: :put do |f| %&gt;
  </code></pre>
  
  <h3>routes:</h3>
  
  <pre><code>namespace :any do
    namespace :things do
      put '/criar', to: 'user#criar'         # problem with XHR
      put '/atualizar', to: 'user#atualizar' # problem with XHR
    end
  end
  
  resources :anything   # this one works fine with XHR
  </code></pre>
  
  <p>As you can see in the test.log, <code>Processing by UserController#criar as</code> don't have a specific format (maybe that is the problem?).</p>
  
  <h3>test.log:</h3>
  
  <pre><code>Processing by UserController#criar as 
    Parameters: { ... }
  </code></pre>
  
  <h3>error message:</h3>
  
  <pre><code>Failure/Error:
    respond_to do |format|
      if @user.update_attributes(user_params)
        format.js {
          flash[:success] = "Success!"
          redirect_to root_path
        }
      else
        format.js 
      end
  
  ActionController::UnknownFormat:
  ActionController::UnknownFormat
  </code></pre>
  
  <h3>Another request test</h3>
  
  <pre><code>it "should be redirect to (criar)" do
    put user_criar_path(1), xhr: true
    expect(response).to redirect_to(new_session_path)
    expect(request.flash_hash.alert).to eq "To continue, please, sign in."
  end
  </code></pre>
  
  <h3>Error message</h3>
  
  <pre><code>Failure/Error: expect(response).to redirect_to(new_session_path)
    Expected response to be a &lt;3XX: redirect&gt;, but was a &lt;401: Unauthorized&gt;
    Response body: To continue, please, sign in.
  </code></pre>
  
  <p>Observations:</p>
  
  <ul>
  <li>I already try to changing the url on routes to: 
  <code>put '/criar', to: 'user#criar', constraints: -&gt; (req) { req.xhr? }</code></li>
  <li>As I said before, I was doing the same thing (tests, controller) to others resources using XHR from <code>form_with</code> and they are working fine. This one with a custom url that didn't work.</li>
  <li>Rails 5.2 and Rspec 3.6</li>
  <li>Any question, just ask on comments</li>
  </ul>
  
  <p>Thanks in advance!</p>