Can't get rails search form to work with simple_form and sunspot

Josh Frank picture Josh Frank · Apr 1, 2013 · Viewed 7k times · Source

I have a simple rails app that uses the simple_form gem for forms and I am trying to set up a search form using sunspot. I have followed the instructions in Ryan Bates' railscast on setting up sunspot and it works fine except for the fact that I know I am not using the simple_form helpers and I would like to. Here is the code for the search form:

From _search.html.erb:

<%= form_tag rules_path, :method => :get do |f| %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
<% end %>

From rules_controller.rb:

def index
  @search = Rule.search do
    fulltext params[:search]
  end
  @rules = @search.results

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @rules }
  end
end

Like I mentioned this works for now but the question is:

How can I alter the _search.html.erb for to use the simple_form format?

Answer

Mundo Calderon picture Mundo Calderon · Jul 24, 2013

I was able to get it working by giving the form a symbol the same name as the input and specifying the URL. Take a look at the URL your form_tag produces and what simple_form_for produces with just a path. So try this in your form partial:

<%= simple_form_for :search,  url: rules_path , :method => :get do |f| %>
    <%= f.input :query, :autofocus => true %>
    <%= f.submit "Search" %>
<% end %>

if that doesn't work, study what your form_tag produces and try to replicate that with simple_form.