I have a search form that works well using the pg_search gem. I basically adapted it from Railscast #343.
However, it always reloads the page on search, whatever I try.
I have tried adding :remote => true to the form and put in a respond_to block in the controller with format.js. I have also tried creating a seperate '/search' path in routes that only returns js but my page always reloads with the params in the url.
Here's the relevant bits of code:
Form:
<div id="search_controls">
<form class="form-inline">
<div class="form-group">
<%= form_tag discover_path, :method => 'get', id: "tapes_search" do %>
<%= text_field_tag :query, params[:query], :autocomplete => :off, id: 'search_field', class: "form-control" %>
<%= submit_tag "Search", :name => nil, id: 'search_submit', class: 'btn btn-default' %>
<% end %>
</div>
</form>
</div>
</div>
Controller:
def index
if params[:query]
@tapes = Tape.search(params[:query]).order("release_date DESC", :id).paginate(:page => params[:page], :per_page => 15)
else
@tapes = Tape.order("release_date DESC", :id).paginate(:page => params[:page], :per_page => 15)
end
end
Modal:
include PgSearch
pg_search_scope :search, against: [:album_name, :artist_name],
using: {tsearch: {dictionary: "english"}},
ignoring: :accents
def self.text_search(query)
search(query)
end
Route
match '/discover', to: 'tapes#index', via: 'get'
This is obviously as it stands without any ajax functionality. Would anyone mind showing me what I'd have to do for the params to be submitted discreetly without a page reload and js to be triggered in response?
I've used ajax all over my site, not sure what's holding me up here :( Thanks!