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">

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

<% if [email protected]? %>
  errors = <<ERR
  <p>Please fix the following errors:</p>
    #{@category.errors.collect{|err| "<li>" + err[0] + " " + err[1] + "</li>" } }
$('#error-message').html('<%= escape_javascript(errors) %>');
<% else %>
<% 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:

Bad bad PHP code

Read this blog entry by 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}")

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

  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 } 

    true  # so we can do "render_404 and return"

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

OAuth2 errors

Tagged invalid_grant, invalid_request, error, oauth2  Languages bash

OAuth2 errors

The list of OAuth2 errors can be found from the RFC:

       The request is missing a required parameter, includes an
       unsupported parameter value (other than grant type),
       repeats a parameter, includes multiple credentials,
       utilizes more than one mechanism for authenticating the
       client, or is otherwise malformed.

       Client authentication failed (e.g., unknown client, no
       client authentication included, or unsupported
       authentication method).  The authorization server MAY
       return an HTTP 401 (Unauthorized) status code to indicate
       which HTTP authentication schemes are supported.  If the
       client attempted to authenticate via the "Authorization"
       request header field, the authorization server MUST
       respond with an HTTP 401 (Unauthorized) status code and
       include the "WWW-Authenticate" response header field
       matching the authentication scheme used by the client.

       The provided authorization grant (e.g., authorization
       code, resource owner credentials) or refresh token is
       invalid, expired, revoked, does not match the redirection
       URI used in the authorization request, or was issued to
       another client.

       The authenticated client is not authorized to use this
       authorization grant type.

       The authorization grant type is not supported by the
       authorization server.