title: How to use an ActiveModel with resourceful URL helpers?
body: <p>A confusing question title requiring a lot of explanation for what I hope isn't actually too difficult a problem.</p>
  
  <p>I have created a little ActiveModel (<code>FooBarSearch</code>) to handle search requests for an ActiveRecord model (<code>FooBar</code>) in my Rails 3.2.16 application. There are a few places in the site I would like my users to be able to search <code>FooBar</code> so I want to be able to use URL helpers, but I am having trouble getting it all working.</p>
  
  <p>I suspect the issue is me having to define some sort of <code>to_param</code> or <code>to_query</code> method in <code>FooBarSearch</code>. But can not find a definitive solution or guide. Any help would be greatly appreciated.</p>
  
  <h2>What I want to be able to write</h2>
  
  <pre><code>- @request = FooBarSearch.new :option =&gt; 'value'
  = link_to 'download csv', search_foo_bar_path(@request, :format =&gt; :csv)
  </code></pre>
  
  <h2>What this currently produces</h2>
  
  <pre><code>&lt;a href="/foo_bar/search"&gt;download csv&lt;/a&gt;
  </code></pre>
  
  <h2>What I want to see</h2>
  
  <p>The following is what I see in the address bar in Chrome when the form submits, and so (I assume) this is what I want the URL helper to produce</p>
  
  <pre><code>&lt;a href="/foo_bar/search.csv?utf8=✓&amp;foo_bar_search%5Boption_1%5D=value&amp;foo_bar_search%5Boption_2%5D=value_2&amp;commit=Search"&gt;
  </code></pre>
  
  <hr>
  
  <h2>Edit</h2>
  
  <p>After a lot of keyboard bashing i seem to have come up with something that works as i detailed above. The only problem is i don't really know <em>why</em> it works? Can anyone explain?</p>
  
  <h2>app/models/concerns/foo_bar_search.rb</h2>
  
  <pre><code>  def attributes
      { :option_1 =&gt; @option_1, :option_2 =&gt; @option_2, :option_3 =&gt; @option_3 }
    end
  
    def to_param
      attributes.to_param
    end
  
    def to_query(key = 'foo_bar_request')
      attributes.to_query(key)
    end
  </code></pre>
  
  <hr>
  
  <h2>Existing code</h2>
  
  <h2>/app/controllers/foo_bars_controller.rb</h2>
  
  <pre><code>class FooBarController &lt; ApplicationController
    def
      if params[:foo_bar_search]
        @request = FooBarSearch.new params[:for_bar_search]
      else
        @request = FooBarSearch.new
      end
  
      respond_to do |format|
        format.html
        format.csv { @request.to_csv }
      end
    end
  end
  </code></pre>
  
  <h2>/app/views/foo_bars/search.html.haml</h2>
  
  <pre><code>%h1= @page_title = "Foo Bar Search"
  
  = form_for(@request, :url =&gt; search_foo_bar_path, :method =&gt; :get) do |f|
    .form_section
      %table
        %tr
          %th Search Option 1
          %td= f.select :some_option_id
      = submit_tag 'Search!'
  </code></pre>
  
  <h2>config/routes.rb</h2>
  
  <pre><code>  resources :foo_bar do
      collection do
        get :search
        post :search
      end
    end
  </code></pre>
  
  <h2>app/models/concerns/foo_bar_search.rb</h2>
  
  <pre><code>class FooBarSearch
    extend ActiveModel::Naming
    include ActiveModel::Conversion
  
    # Required by ActiveModel::Conversion
    def persisted?
      false
    end
  
    def initialize(options = {})
      # ...
    end
  
    # I think i may need #to_param and/or #to_query here, 
    # but so far have not been able to get this working...
  end
  </code></pre>
  
  <p>Thanks for any help</p>