I have a working search mechanism on my page; a form with a text field and 'search'-labelled submit button.
I’ve also got an animated raphael.js-based item on my page, with clickable items on it that I want to result in an executed search.
On click, I send to my controller params data intended to be search terms taken from the raphael objects.
The data is being passed, and the controller code runs, but the corresponding view fails to render so the click appears to the user to do nothing.
So on click, I make an Ajax call like this:
File searchthing.js
// Add a search function
circle.click(function (e) {
var csrch = this.data('label');
// alert('clicked ' + csrch);
e.preventDefault();
$.ajax({
type: "GET",
url: "/bubbleBar",
data: $.param({ srchterm: csrch} ),
success: function(){
// window.location.replace("/pictures");
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("--- error: " + errorThrown);
}
});
});
The ‘bubbleBar’ action in my controller fires successfully, transfers the search terms, and redirects to my working index action, which handles searches:
File myController.rb:
def bubbleBarSearch
redirect_to pictures_path(:srchterm => params[:srchterm])
end
def index
# Initialize the pictures array
#
@pictures = Picture.first
if params[:srchterm].present?
# If the search button was hit, search pictures for the terms
@pictures = []
results = Picture.search(params[:srchterm], page: params[:page])
results.each do |x|
@pictures << x
end
# Also search keywords for the search terms
#
keywords = Keyword.search(params[:srchterm], page: params[:page])
# Retrieve images based on keywords
#
keywords.each do |kw|
pics = Picture.where(:id => kw.picture_id)
pics.each do |pic|
# Only add to results if unique
#
if !@pictures.include? pic
@pictures << pic
end
end
end
respond_to do |format|
format.html {
puts '---------------- ding -----------------'
# Redirect_to pictures_path
}
format.js {alert('dong');}
end
else
@pictures = Picture.all.order("RANDOM()")
end
end
The problem I’m having is that Ajax returns success, but the corresponding view for the ‘index’ action does not render.
If I uncomment the 'window.location.replace("/pictures”);' line in my JavaScript code, I can force the render on success, but I lose the search results that the controller code got for me. If I uncomment the 'redirect_to pictures_path' line in my controller, I of course get an infinite loop that errors out.
Research I’ve do done so far tells me I should be catching the response in the controller.
I’ve not worked with respond_to before, and it appears that format.html gets hit with the Ajax call, but ALSO fires when I manually do the search using the search field/button mechanism already set up on the page. I'm unclear as to why my Ajax call comes in as HTML rather than JavaScript.
So that means if I handle the rendering of the view manually in the controller, I’m not catching the case when the search hasn’t come through the already working mechanism and running the risk of rendering a page twice or otherwise introducing malfunction into my site.
I’m lost on how to make my click/Ajax call successfully render the desired view from the click function after the controller action happens. What am I missing here? What is the ideal methodology to achieve this, and not lose the controller data I need to successfully render?
Outputs from Ruby on Rails indicate that the server believes it is rendering the proper Haml view file, however I placed a JavaScript tag at the bottom of the file to log output to console and the console is empty after a click. If I navigate to the same view through the site, the statement is output to the console.
I've modified my Ajax success callback to have a parameter, and upon examination the variable contains the HTML code for the view. So is Ruby on Rails directing the output to Ajax instead of the browser? How can I then render that output from the Ajax success callback?
Successful calls to the controller contain a utf8 parameter that is not in my Ajax call. Is this a factor?