First - I have read ALL of the other threads on SO with the same error. NONE of them helped me one bit.
Flow:
- On new Customer page, fill out fields, click save
- Fire off method in customer_controller to create
Render json with info back to the page:
format.json {render :json => res.to_a }
Page gets the array :
Array[2] new:851
It's a properly-formatted array.
Error:
Uncaught TypeError: Cannot read property 'top' of undefined
Trace the source:
$.ajax({
type : "POST",
url : "/customers",
data : dataString,
dataType : "json",
success : function(data) {
console.log(data);
// data = data[0];
// console.log(data);
if (data[0] == 'error' || data[0] == 'i') {
$("#flash_message").addClass("alert-error");
$("#flash_message").html("<p>" + data[1] + "</p>");
$('html,body').animate({
scrollTop : $("#flash_message").offset().top
}, 'slow');
} else {
$("#flash_message").addClass("alert-success");
$("#flash_message").html("<p>" + data[0] + "</p>");
$('html,body').animate({
scrollTop : $("#flash_message").offset().top
flash_message eh? Let's see in the view:
<% flash.each do |key, value| %>
<div id="flash_message" class="alert alert-<%= key %>">
<%= value %>
Yup, definitely a flash_message id in there.
However, we are using layouts - so there must be a timing issue. The AJAX code is in admin.html.erb layout, and we're completing our NEW customer action from the customer.html.erb view. Customer Controller has:
layout 'admin', :except => [:show]
"show" is not our method in question here - we're using "create"
Processing by CustomersController#create as JSON
So why in the blue-bloody-blazes do we not know what "flash_message" is ?