Hoopla!

now with extra whiz-bang!

Hoopla!

undefined method 'dom_class'

February 23, 2007 · 0 comments

I've had a weird situation over here at StudioDanger headquarters with plugins going haywire. I had my app working real slick and then installed the new Engines plugin and the slickness continued unabated. A subsequent installation of the simply_helpful plugin gave me a crazy error:

"undefined method 'dom_class'"

There's this line in simplyhelpful where ActionView::Helpers::FormHelper is monkey-patched to call domclass and dom_id. These should be available because in the plugin's init.rb we see the SimplyHelpful helper injected into ActionController as a helper

require 'simply_helpful'
ActionController::Base.send :include, SimplyHelpful::RecordIdentificationHelper
ActionController::Base.helper SimplyHelpful::RecordIdentificationHelper,
                              SimplyHelpful::RecordTagHelper

At some point 'self' became an unmodified version of ActionController while simply_helpful was looking for it's special helper functions. Why did it to that? I spent three hours trying to figure it out. I have no idea.

You can patch it though. Just change domid and domclass to SimplyHelpful::RecordIdentifier.domid and SimplyHelpful::RecordIdentifier.domclass. That'll do the trick.

→ 0 comments Tags:

Rails: Custom 404 Pages

August 05, 2006 · 4 comments

I launched JCFootballProspects.com a while ago and things have been going smoothly but the site is stuck with the nasty black-on-white error messages when somebody types in a wrong address.

I’m now at the point where I want to give out some fancy 404 pages and I’ve been looking around at what other folks have done. I’m amazed to find that all the examples I can find have a 404.html page in the `#{RAILS_ROOT}/public` directory. I’m sure it’s effective to just have a plain-html file that gets served up for errors (and it’s certainly a lot better than the default ugly stuff) but I’m convinced there are advantages to having application-provided 404 pages as well.

There are three different kinds of errors I trap and respond to: * A user gives incorrect or insufficient parameters to a page view * A user attempts to access an action that doesn’t exist * A user attempts to access a controller that doesn’t exist

The first is easily handled – even the AWDWR book shows in an early example that you can output a flash message if something goes wrong with a request. The second is only slightly more complicated but, luckily, Ruby has a great way to respond to missing actions on controllers using method_missing. The solution to the third is provided by Rails’ routing capabilities.

How to hook up an app-driven 404 page: ==== The first thing I did was added a view to my default controller (mine is called ‘home’). I created ./app/views/home/404.rhtml and put just some basic stuff in it:
<%= content_tag 'h2', 'Whoops!' %>

<%= content_tag 'h3', 'Page not found' %>

[insert message here]

The next step was to add method_missing to my application controller:
1
2
3
4
5
6
7
8
9
10
11
class ApplicationController < ActionController::Base

  ...

  def method_missing(methodname, *args)
   @methodname = methodname
   @args = args
   render 'home/404', :status => 404
  end

end
And one super low priority route (put it at the very bottom) finishes off the job:
  map.error ':controllername',  :controller => 'home',
                              :action => '404'

→ 4 comments Tags:

Rails Community Forum

June 15, 2006 · 1 comment

I’ve never been much into forums but I’ve really started pouring my heart into learning Rails and there’s been a notable lack of community outside the upper echelons of development.

That’s why I was happy to discover a link from Ruby Inside that announced the launch of Rails Forum.com. I’ve only spent a little time there so far but it’s nice to be one of the first people to sign up and to be involved with it from the start.

RailsForum.com is a great place to go to ask questions that you can’t find the answers to elsewhere. Getting random 500 errors? They’ll help you out. Not sure what this whole RJS thing is about? Well, we’re all trying to figure that out together :-)

If you’re into Rails or think you might want to be – I’ll see you there!

→ 1 comment Tags:

has_one :through » ArgumentError: Unknown key(s): through

June 01, 2006 · 2 comments

  1. Problem:

You’re getting something that says “ArgumentError: Unknown key(s): through” when you’re using a has_one :something, :through => :something else code.

  1. Explanation:

The :through option for Rails join models is really, really handy. Basically, it allows you to turn `guy.thing.places` into `guy.places` without having to change anything in the database.

How the models would be setup to allow for the more tedious way to associate places with a guy.
class Guy < ActiveRecord::Base
  has_one :thing
end

class Thing < ActiveRecord::Base
  belongs_to :guy
  has_many   :places
end

class Place < ActiveRecord::Base
  belongs_to :thing
end

The only alteration needed is turning the Guy class into:

class Guy < ActiveRecord::Base
  has_one  :thing
  has_many :places, :through => :thing
end
For a better example of how to use :through check out Ariejan on Rails The problem with this, and I don’t know why this is, is that this doesn’t work with has\_one – only has\_many! If we were to change Thing to only have one Place we would then alter the joins thusly:
class Guy < ActiveRecord::Base
  has_one  :thing
  has_one  :place, :through => :thing
end

class Thing < ActiveRecord::Base
  belongs_to :guy
  has_one    :place
end
And our code would fail! The above code will barf out an `ArgumentError: Unknown key(s): through` error.
  1. Solution:
You can use a has\_many join and make it act like a has_one join. Just use:
class Guy < ActiveRecord::Base
  has_one  :thing
  has_many :place, :through => :thing
end
Then, when you call guy.place you’ll get an array (with just one element) of places. You can use `guy.place.first` to replace `guy.thing.place`

→ 2 comments Tags:

Application Error - Rails application failed to start properly

April 09, 2006 · 19 comments

Okay, I wish I could write this now and then go back in time two days, read it, and save myself a lot of hassle. I spent a lot of time trying to debug a rails installation on a shared host and it could have been so much easier.

  1. Get your Rails app working
  1. step one
Set file permissions. Do it like so: ./public needs to be set to 755
$ chmod 755 public
./public/dispatch.fcgi (or dispatch.cgi – but don’t use that!) also to 755
$ chmod 755 public/dispatch.fcgi
make sure ./public/.htaccess exists (my copy can be found at the bottom of this post) ./log and everything under it needs to be set to 766
$ chmod -R 766 log
do the same for the ./tmp directory (Rails writes session data here)
$ chmod -R 0666 tmp
#### step two Make sure you’re running in ‘development’ mode. Code errors will be displayed to the screen in this mode – but ‘production’ will just barf out on you (it’s intended to work this way). Set RAILS_ENV=’development’ in config/environment.rb if you have no other way to do this.
$ nano config/environment.rb
#### step three If the above didn’t work, look at the bottom of any of your log files. Depending what environment you’re in you may need development.log or production.log. If it gives you a handy error like ‘could not find environment.rb’ or something like that it’ll really help you figure out the problem.
$ tail -n 100 log/development.log
#### step four Get into the ruby console. Try to run a command like you would in your app like User.find_all or something similar. This will show you errors that wouln’t come up elsewhere.
$ ./script/console 
  1. step five If you’re running an app that requires a specific version of Rails but didn’t include it along with the app you’ll need to ‘freeze’ Rails into your app. This is actually quite simple – if you put the whole Rails source code into ./vendor/rails then your application will use that code instead of the stuff on your server at /usr/lib/ruby/ The way to do this is to run any of the following:
rake freeze_gems
(get the latest stable version)
rake freeze_edge
(get the cutting-edge version)
svn co -r 997 http://dev.rubyonrails.org/svn/rails/trunk
(insert revision number you want instead of 997)
  1. good luck!
### Resources: My working .htaccess file:
AddHandler fastcgi-script .fcgi
AddHandler cgi-script .cgi
Options +FollowSymLinks +ExecCGI

RewriteEngine On
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

ErrorDocument 500 /500.html

#ErrorDocument 500 "

Application error

Rails application failed to start properly"

If these steps didn’t work for you leave a comment and I’ll try to help you out. I’m a GNU guy so I don’t know much about Rails on Windows, but I’d be happy to help you avaoid the hideous, hideous Rails errors.

Also, if you’re looking at random 500 errors while working on your Dreamhost account, you’ll want to use a modified dispatch.fcgi file.

→ 19 comments Tags: