I'm using Ransack in my Rails 5.2 app to search using a filter. It uses the Index action in a controller and renders results correctly until I try create a second filter in another action in the same controller. Once I use the filter in the the view of the new action it shows me that the filter is still being processed using the original action.
I'll add original and new action/views below. Can't seem to tell ransack to specifically process a particular action.
Original Working code
Events#Index
def index
@category_list = Category.all.order("name ASC")
# Get non expired events
@events = Event.where("expiry > ?", Time.now)
@events = @events.all
@events = @events.paginate(:page => params[:page], :per_page => 12).order("created_at DESC")
#ransack
@search = @events.ransack(params[:q])
@data = @search.result(:distinct => true)
@events = @data.to_a
end
Index view (just the bits that are relevant)
<%= search_form_for([:admin, @search], remote: true ) do |f| %>
<div class="wrapper" id="event_listing">
<%= render partial: "/admin/events/events-list", locals: {events: @events} %>
</div>
So index page renders a partial with the results @events
index.js.erb
$('#event_listing').html('<%= j render partial: "/admin/events/events-list", locals: {events: @events} %>');
$('.collapsible').collapsible();
All works fine.
Now if I add a new action called expired_events with code
def expired_events
@category_list = Category.all.order("name ASC")
# Get expired events
@events = Event.where("expiry < ?", Time.now)
@events = @events.all
@events = @events.paginate(:page => params[:page], :per_page => 12).order("created_at DESC")
#ransack
@expired_search = @events.ransack(params[:q])
@data = @expired_search.result(:distinct => true)
@events = @data.to_a
end
I've also my route a new views
routes.rb
resources :events do
collection do
get 'destroy_multiple_images'
get 'destroy_single_image', action: "destroy_single_image"
get 'expired_events'
end
end
expired_events.html.erb
<%= search_form_for([:admin, @expired_search], remote: true ) do |f| %>
<div class="wrapper" id="event_listing">
<%= render partial: "/admin/events/events-list", locals: {events: @events} %>
</div>
the main point here is telling the search form about the different variable in the admin namespace. I presume the render will look for the same name in js like below.
expired_events.js.erb
$('#event_listing').html('<%= j render partial: "/admin/events/events-list", locals: {events: @events} %>');
$('.collapsible').collapsible();
Now Rails shows me in the server that it's still processing the index.js.erb file when I hit the filter button from the expired_events view page. Does anyone know how to specifically tell it to render the right js search form?
I'm nearly sure the variables in the partials rendered do not make a difference as if I can get Ransack to run from the right search form the results should change depending on the action.
screesnhot from when I am on the expired_events page
Then when I use the filter I get the results showing the filter still using the index.js file.
Any help would be appreciated. Thanks everybody.
Got it. Thanks.
If anyone has a cleaner looking or more correct way of doing it I'm all ears thanks. Specifically able to tell the form to use the action like below.
<%= search_form_for([:admin, @expired_search], url: url_for(controller: 'events', action: 'expired_events'), remote: true ) do |f| %>