title: Rails: Routing error when rendering AJAX partial
body: <p>I am encountering a routing error when I try to render a partial in an ajax call:</p>
  
  <pre><code>Routing Error
  
  No route matches {:action=&gt;"destroy", :controller=&gt;"relationships", :user_id=&gt;#&lt;User id: 2, username: .....
  </code></pre>
  
  <p>Within my app, I have a list of followers for a user displayed on the profile page. Instead of paginating the followers, I would like to try to return the next offset of followers from the server through AJAX. My view already utilizes partials for displaying a list of these followers (limited to 5 records). </p>
  
  <p>My goal is to use an AJAX call to return this partial with the next offset of records formated (I haven't implemented the functionality to return offset records yet - I'm just trying to get the ajax working first). The partials work fine when I visit the profile page in my browser (and view the first 5 records), the error occurs when I make the AJAX call.</p>
  
  <p>Here is the form in the view where the ajax call originates:</p>
  
  <pre><code>&lt;%= form_tag user_relationships_path(@user), method: :get, remote: true do %&gt;
      &lt;%= submit_tag 'load more...' %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>Here is the route:</p>
  
  <pre><code>resources :users, only: [:index, :show, :new, :create, :edit, :update, :destroy] do
    resources :relationships, only: [:create, :destroy, :index]
  end
  </code></pre>
  
  <p>Here is my controller action (<code>relationships#index</code>) which responds to the request:</p>
  
  <pre><code>def index
    @user = User.find_by_username(params[:user_id])
      respond_to do |format|
        format.js { render 'load_followers' }
    end
  end
  </code></pre>
  
  <p>The load_followers.js.erb partial:</p>
  
  <pre><code>$('ul#followers').append("&lt;%= render 'users/following_items', users: @user.followers %&gt;")
  </code></pre>
  
  <p>The users/following_items.html.erb partial:</p>
  
  <pre><code>&lt;% users.each do |user| %&gt;
  &lt;li class="clearfix"&gt;
      &lt;div class="box-gravatar pull-left"&gt;
         &lt;%= link_to user do %&gt;
         &lt;%= gravatar_for user, 40 %&gt;
         &lt;% end %&gt;
      &lt;/div&gt;
      &lt;div class="pull-right"&gt;
         &lt;%= render 'relationships/follow', user: user %&gt;
      &lt;/div&gt;
      &lt;%= link_to user.username, user %&gt;
      &lt;div class="box-author"&gt;joined &lt;%= join_date_for user %&gt;&lt;/div&gt;
  &lt;/li&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>And finally the relationships/follow.html.erb partial:</p>
  
  <pre><code>&lt;% unless current_user?(user) %&gt;
     &lt;% if current_user.following? user %&gt;
        &lt;p&gt;&lt;%= link_to 'unfollow', user_relationship_path(user), method: :delete, class: "btn" %&gt;&lt;/p&gt;
     &lt;% else %&gt;
        &lt;p&gt;&lt;%= link_to 'follow', user_relationships_path(user), method: :post, class: "btn btn-primary" %&gt;&lt;/p&gt;
     &lt;% end %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>I have tracked down the offending code to the relationships/follow.html.erb partial. When that is removed, the ajax call works fine and the partial is appended to the end of the <code>ul</code>. Clearly it has to do with rails having an issue with the <code>link_to</code> to the <code>relationships#destroy</code> method - however, nothing I've tried seems to work. </p>
  
  <p>Edit: Here are the results of running <code>rake routes</code>:</p>
  
  <pre><code>root        /                                                posts#index
              posts_test        /posts/test(.:format)                            posts#test
                  submit        /submit(.:format)                                posts#new
                  signup        /signup(.:format)                                users#new
                   login        /login(.:format)                                 sessions#new
                  logout DELETE /logout(.:format)                                sessions#destroy
                   about        /about(.:format)                                 about#index
                  search        /search(.:format)                                search#index
                sessions POST   /sessions(.:format)                              sessions#create
             new_session GET    /sessions/new(.:format)                          sessions#new
                 session DELETE /sessions/:id(.:format)                          sessions#destroy
           post_comments POST   /posts/:post_id/comments(.:format)               comments#create
              post_votes POST   /posts/:post_id/votes(.:format)                  votes#create
                   posts GET    /posts(.:format)                                 posts#index
                         POST   /posts(.:format)                                 posts#create
                new_post GET    /posts/new(.:format)                             posts#new
                    post GET    /posts/:id(.:format)                             posts#show
      user_relationships GET    /users/:user_id/relationships(.:format)          relationships#index
                         POST   /users/:user_id/relationships(.:format)          relationships#create
   new_user_relationship GET    /users/:user_id/relationships/new(.:format)      relationships#new
  edit_user_relationship GET    /users/:user_id/relationships/:id/edit(.:format) relationships#edit
       user_relationship GET    /users/:user_id/relationships/:id(.:format)      relationships#show
                         PUT    /users/:user_id/relationships/:id(.:format)      relationships#update
                         DELETE /users/:user_id/relationships/:id(.:format)      relationships#destroy
                   users GET    /users(.:format)                                 users#index
                         POST   /users(.:format)                                 users#create
                new_user GET    /users/new(.:format)                             users#new
               edit_user GET    /users/:id/edit(.:format)                        users#edit
                    user GET    /users/:id(.:format)                             users#show
                         PUT    /users/:id(.:format)                             users#update
                         DELETE /users/:id(.:format)                             users#destroy
              categories GET    /categories(.:format)                            categories#index
                         POST   /categories(.:format)                            categories#create
            new_category GET    /categories/new(.:format)                        categories#new
                category GET    /categories/:id(.:format)                        categories#show
                                /:category(.:format)                             posts#index
  </code></pre>
  
  <p>Thanks!</p>