I have a semantic_form_for in my view that sets up a search filter, and more filters are added with a button that, through JavaScript, adds more filters.
<%= semantic_form_for current_coach, :remote => true, :html => { :class => "custom-form rb-form", :id => "filter", :'data-url' => "#{roster_builder_coach_index_path}" } do |f| %>
<div class="filter_parent_container">
<% @search.conditions.each do |condition| %>
<%= render "filter", :condition => condition %>
<% end %>
</div>
<div class="bottom-info">
<div class="count">
<p class="num"><%= @athletes.count %></p>
<p>identified with this filter set</p>
</div>
<input type="submit" class="send-request" id="search-button" value="" />
<input type="button" class="add_filter" value="Add Another Filter" />
</div>
<div class="cl"> </div>
<% end %>
I need the form to submit via ajax, but it isn't. I'm checking the params in the controller and they are:
{"action"=>"roster_builder", "controller"=>"coach"}
Here's the controller (logger is checking the params):
def roster_builder
authorize! :access_roster_builder, current_coach
logger.info("**************");
logger.info("**************");
logger.info("**************");
logger.info("**************");
logger.info("**************");
logger.info(params);
@search = Search.new(sport: current_coach.primary_sport, conditions: params[:search])
@athletes = @search.query.page(params[:page]).per_page(8)
render "athletes" if request.format.js?
end
Here is the Ajax call:
$("#filter").submit(function(){
$.ajax({
url: $("#filter").attr("data-url"),
type: 'POST',
success: function(data) {
$(".results")[0].parentNode.removeChild($(".results")[0]);
}
});
});
Before I used :remote => true, it was working, but I need persistence across the filters (including the ones added via JS), so I need Ajax to perform the search rather than doing a page refresh. Why isn't params working now that Ajax is sending the form data? I even tried sending the data via Ajax as serialized, to no avail, as follows:
$.ajax({
url: $("#filter").attr("data-url"),
type: 'POST',
data: $("form#filter").serialize(),
success: function(data) {
$(".results")[0].parentNode.removeChild($(".results")[0]);
}
});
Any help would be appreciated!