Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 546
Josh Frank Avatar asked Apr 01 '13 19:04

Josh Frank


3 Answers

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.

like image 136
Mundo Calderon Avatar answered Sep 30 '22 06:09

Mundo Calderon


Have you tried the following:

<%= simple_form_for rules_path, :method => :get do |f| %>
  <%= f.input :search %>
  <%= f.button :submit %>
<% end %>
like image 33
Luís Ramalho Avatar answered Sep 30 '22 04:09

Luís Ramalho


For to do this I'm using simple_form the next way:

1. Define a form(I use haml)

=simple_form_for :search, url: search_path(:search), :method => :get  do |f|
 =f.input :q
 =f.submit "Search"

2. Define a route in routes.rb

get '/jobs/search/:q', :to => 'jobs#search', :as => :search

3. Define a method inside your controller (in my case jobs_controller.rb)

  def search
   @search = params[:search][:q]
  end
like image 43
Nelson Patricio Jimenez Avatar answered Sep 30 '22 04:09

Nelson Patricio Jimenez