title: backbone add a search route to fetch (Rails, coffeescript, backbone)
body: <p>In my backbone view I am calling fetch on a collection like this:</p>
  
  <pre><code>@collection.fetch
        data: 
          query: query
        success: (collection, response) =&gt;
          #do stuff
  </code></pre>
  
  <p>This works great if I just want to call spots#index in my rails routes. But I want to set up a "search" route in my spots_controller.rb that returns a different set of results. How do I set this up.</p>
  
  <p>Here is my model and collection:</p>
  
  <pre><code>class App.Models.Spot extends Backbone.Model
  
    initialize: -&gt; 
  
    url: -&gt;
      if @isNew()
        "#{App.getURL()}/spots.json"
      else
        "#{App.getURL()}/spots/#{@id}.json"
  
    urlRoot: -&gt;
      App.getURL() + "/spots.json"
  
  class App.Collections.SpotsCollection extends Backbone.Collection
    model: App.Models.Spot
  
    initialize: (models, options) -&gt;
  
    url: -&gt;
      "#{App.getURL()}/spots.json"
  </code></pre>
  
  <p>Here is the router for a spot:</p>
  
  <pre><code>class App.Routers.SpotsRouter extends Backbone.Router
    routes:
      '' : 'index'
  
    initialize: -&gt;
      @collection = new App.Collections.SpotsCollection()
      @collection.fetch()
  
    index: -&gt;
  </code></pre>
  
  <p>In my rails backend I have a simple controller, here is spots_controller.rb</p>
  
  <pre><code>class SpotsController &lt; ApplicationController
  
    def index
  
      @spots = Spot.all
  
      respond_to do |format|
        format.html # index.html.erb
        format.json { render json: @spots }
      end
    end
  end
  </code></pre>
  
  <p>I'm just curious how I would add a search method, so when I call fetch, I could tell it to fetch all the spot records based on some search terms. I know I could just add some data params to the call to my spots#index action, then in the index action find those terms, but, it seems like it would be better to tell fetch to fetch the records from spots#search. What is the best way to do this? Thanks!</p>