Having a nightmare with this one and think i'm going to go crazy. It must be something simple. I have ransack working on my listings#index. It renders the results in a JS partial no problem using remote: true in the search form. All good. however....
I want this same functionality to work on the root_page of the app. So essentially a search bar that will show the search results in a partial just under the bar when a user searches. I can get this to work normally with ransack whereby the page refreshes and the results show from the partial. It will not for the life of me render the partial as JS just like how the listing controller does it.
The code bits are pretty much similar. I'll add them both below. Ransack works, just doesn't recognise the remote: true bit. I've looked into inspect on chrome and can actually see that for some reason the form class doesn't register correctly. I'll screenshot both working and non working bits.
So it's just a matter of the Ransack search query not recognising the remote: true by the looks of it. Any guesses? Does Ransack require something special to be added to the form to enable it to work from a page named home.html.erb? instead of index.html.erb? Would love a set of eyes to literally tell me i'm doing something stupid here. Thanks.
Working code snippets for searching in the listing_controller
listing#index
def index
@search = Listing.ransack(params[:q])
@data = @search.result(:distinct => true)
@listings = @data.to_a
end
listing index.html.erb
<%= search_form_for @search, remote: true do |f| %>
<%= f.search_field :title_or_content_or_aircraft_highlights_or_airframe_engine_apu_weight_or_avionics_connectivity_or_interior_or_exterior_or_additional_info_cont, placeholder: "Search by keyword..", class: "form-control" %>
<%= f.submit "Show results", class: "btn btn-secondary btn-lg btn-block" %>
<% end %>
<%= render partial: "shared/aircrafts-list" %>
listing index.js.erb
$("#aircraft_listing").html("<%= j render partial: 'shared/aircrafts-list' %>");
Works like a dream.
Pages controller which won't play ball. Search works but not ajax call
pages#home
def home
@search = Listing.ransack(params[:q])
if params[:search].present?
@data = @search.result(:distinct => true)
@listings = @data.to_a
end
end
pages home.html.erb
<%= search_form_for @search, remote: true do |f| %>
<%= f.search_field: title_or_content_or_aircraft_highlights_or_airframe_engine_apu_weight_or_avionics_connectivity_or_interior_or_exterior_or_additional_info_cont, placeholder: "Search by keyword..", class: "form-control-lg form-control-borderless" %>
<%= f.submit "Search", class: "btn btn-secondary btn-lg" %>
<% end %>
<%= render partial: "shared/home-search-list" %>
pages home.js.erb
$("#home_search_list").html("<%= j render partial: 'shared/home-search-list' %>");
alert('Hello Rails');
obviously alert doesn't pop up when search ran.
Partials are named (have just checked them again to make sure i'm not going insane)
_aircrafts-list.html.erb
_home-search-list.html.erb
And then the final bit where I can see the remote clearly not happening in the home page version.
For some reason the form tag is not appearing in the home page example. I've just noticed this part now after the morning of pulling whats left of my hair out. Fresh eyes would be great. Now one other thing regarding Ransack, The fact my routes have resources for listing but i literally have just the root_path connected to my pages controller home action, does this matter with Ransack?
Thanks all.
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)