error snippets

Showing ActiveRecord error messages from jQuery Ajax actions and scripts

Tagged ajax, error, jquery, rails  Languages ruby

The HTML, in a layout file, for example application.html.erb:

<div id="error-message" style="display:none">
</div>

The JavaScript, rendered by for example create.js.erb:

<% if !@category.valid? %>
<%
  errors = <<ERR
  <p>Please fix the following errors:</p>
  <ul>
    #{@category.errors.collect{|err| "<li>" + err[0] + " " + err[1] + "</li>" } }
  </ul>
ERR
%>
$('#error-message').html('<%= escape_javascript(errors) %>');
$('#error-message').show();
<% else %>
$('#error-message').hide();
<% end %>

How to debug memory allocation problems in PHP with Xdebug

Tagged xdebug, php, memory, error, performance  Languages php

Xdebug is a good tool for finding the root cause of memory allocation problems such as the one shown here:

Fatal error: Allowed memory size of X bytes exhausted (tried to allocate X bytes)

First install Xdebug by following the Xdebug installation instructions.

Next surround the code you suspect is causing the problem with the following function calls:

xdebug_start_trace('/tmp/mytrace');
...
Bad bad PHP code
...
xdebug_stop_trace();

Read this blog entry by splitbrain.org for more details

How to fix "Mysql::Error: Duplicate entry '2147483647' for key 3: INSERT INTO xxx"

Tagged mysql, numeric, error, gotcha, migration  Languages ruby

2147483647 is the maximum for an integer column in MySQL, so this error probably means you've exceeded this limit somewhere in your code.

Rails automatically detects the best type for your columns, so be sure to specify the correct limit when creating the column with migrations:

# from activerecord-2.1.1/lib/active_record/connection_adapters/mysql_adapter.rb
        case limit
        when 1; 'tinyint'
        when 2; 'smallint'
        when 3; 'mediumint'
        when nil, 4, 11; 'int(11)'  # compatibility with MySQL default
        when 5..8; 'bigint'
        else raise(ActiveRecordError, "No integer type has byte size #{limit}")
        end

This Rails migration code would create a big integer column:

t.integer :product_id, :null => false, :limit => 8

See the section on Numeric Types in the MySQL documentation for more information.

Custom 404 Rails error pages

Tagged 404, rails, custom, error  Languages ruby

All credit for this snippet is due to Henrik Nyh.

Put this in application_controller.rb:

alias_method :rescue_action_locally, :rescue_action_in_public if RAILS_ENV == 'development'
  
  def render_optional_error_file(status_code)
    if status_code == :not_found
      activate_authlogic
      render_404
    else
      super
    end
  end

  def render_404
    respond_to do |type| 
      type.html { render :template => "errors/error_404", :layout => 'application', :status => 404 } 
      type.all  { render :nothing => true, :status => 404 } 
    end

    true  # so we can do "render_404 and return"
  end

How to fix "Unknown column 'id' in 'where clause'" error when using has_many :through

Tagged has_many, mysql, error  Languages ruby

This error is mostly likely caused indirectly by has_many through, which requires an id column to be present in the join table in a many-to-many relationship:

ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'id' in 'where clause': DELETE FROM brand_posts WHERE id = NULL

The fix is to create the id column, which in this case is done like this:

add_column :brand_posts, :id, :primary_key