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?
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.
Have you tried the following:
<%= simple_form_for rules_path, :method => :get do |f| %>
<%= f.input :search %>
<%= f.button :submit %>
<% end %>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With