"malformed header from script"

So, in testing simplyXiangqi, I found I was getting error pages when I tried certain actions. The actions seemed to happen okay, but I got the standard Rail ‘Application Error’.

Looking in the logs, I was getting an error ‘malformed header from script’. ‘Curious’, I thought. The full error was of the form:
malformed header from script. Bad header=isRed = 1:

It seemed to me that part of my code was appearing as a header when the page was returned.

Eventually, I figured it out. My code still had ‘puts()’ calls in it. I’d added these for debugging during development on WebBrick. In WebBrick, the strings output by ‘puts()’ were written to the command window WebBrick was running under. On my Apache installation, though, this was output as a header. Consequently, I got errors.

I removed the ‘puts’ calls, and it all worked fine.

Comments from my old blog:

Thanks, buddy. Saved my sorry ass. 🙂

By Magnus Bergmark at 18:07:07 Saturday 29th September 2007

"malformed header from script"

Dispatch.cgi and End of File Characters

So, I was setting up my Chinese Chess site (domain name might not have propagated yet), and I had some problems. I kept getting the error ‘Application error: Rails application failed to start properly’. Looking in the logs the error was “Premature end of script headers: dispatch.cgi”. Tracked this down to the Dispatch.cgi file through Google, where other folks seem to have had problems with the EOF character.

I couldn’t figure out how to delete these characters – why is it that ‘vi’ is so popular? – so what I did in the end was create a new rails app and copy the cgi files across into my app. The files, although they appeared to have the same text, had very different file sizes.

Anyway, that seemed to do the trick.

Dispatch.cgi and End of File Characters

Impressed with Rails – again

So, I had need of a little ‘Ajax’. I wanted to provide a brief preview of some content, but without all the nastiness of page refreshes – an ideal candidate for XMLHTTPRequest. So I wrote my own. That was a mistake.

I’m comfortable with Javascript – I think I’m fairly accomplished with it – so I got it done, and it worked. It was only then that I thought ‘I think Rails does Ajax too’. Does it ever! Ten minutes, and I had a Rails Ajax version working (a lot less than my own time, even with my boiler-plate code) and, and this is the best bit, much more readable code! That’s what I call a win. link_to_remote was the useful function in question, and my call is:
link_to_remote( "Show",
:url => { :action => "show_small", :id => game.id },
:update => "js_board",
:before => "document.getElementById('js_board').style.left = get_x(this);
document.getElementById('js_board').style.top = get_y(this)",
:complete => "document.getElementById('js_board').style.visibility = 'visible'" )

Impressed with Rails – again

Progess on Ruby Chinese Chess site

So, I’ve been a bit busy with things, so I don’t have any of my usual interesting links to post. I’ve been getting stuff done with the Rails Xiangqi site that I’m building (Chinese chess to the rest of us). Nothing radical, just plugging away.

My biggest complaint with Rails so far is a lack of books and documentation. I mean, the API docs are pretty good, but I’d like to see more example, more tutorials, and a structured thing like a book would be wonderful.

So, what have I learnt? Well, I’ve used routes.rb to set up the default page for the site. You can also ditch that index.html file from public in your application too. I think I’ve figured out how change passwords in the security thing, which is a bit involved, so sometime I’ll post the code.

Partials – annoyingly – don’t have a ‘general’ folder. You can create one, but you have to specify it each time you call a partial. That sucks a bit.

I built a lot of the user admin stuff – login, signup, etc.. It was made very easy by the Login Generator. Updating users and stuff – well, that was only a slight change to the normal scaffold. The big change was updating the password – but even that wasn’t too bad.

All in all, still impressed by how much you can get done in a short time with Rails.

One thing I’d like to figure out is how to do a persistent Login cookie for Rails. I don’t mind having my session deleted, but having authenticated that user, I’d like to not have to do it again the next time they visit the site.

Progess on Ruby Chinese Chess site

Porting Java to Ruby

Long time ago I wrote a Java engine for figuring out valid Chinese Chess (Xiangqi) moves. I was going to build it into a website, something like Red Hot Pawn. Well, it seems that Ruby might be much quicker to develop the database and presentation parts of a website, so I decided to port my Java code across to Ruby. It was very easy, both being very object oriented. It works just fine. Now, all I have to do is the site itself – which should be quick in Ruby.

Porting Java to Ruby

Ruby on Rails on Apache

I just got Rails working on Apache, which is pretty cool, and fairly straight forward really. My big complaint about the whole process is, though, that the tip below was necessary to make apache stop just returning errors…

Note from porter.ea:

One additional edit that I found necessary was to add “#!c:/pathto/rubybin/rubyw” (note the “w”) at the top of the “C:appnamepublicdispatch.fcgi” file.

It’s worth noting the bit about (for performance):
FastCgiConfig -maxClassProcesses 1 -maxProcesses 1 -minProcesses 1 -processSlack 1

Ruby on Rails on Apache

Ruby on Rails

I was ill earlier this week, so out of boredom I thought I’d have a look at Ruby on Rails. Very impressed…

I guess I hadn’t realised, but Ruby is the language, and Rails is the web development framework for it. And boy does it work well. More on that later.

Installation was a bit of a bugger, and I couldn’t get the default installation (which I got here to talk to mySQL at first. The tutorial I was following was Rolling with Ruby on Rails, but I found I had to install the Ruby/MySQL API instead of whatever was installed by default. After that, the tutorial all worked fine.

It’s a good tutorial, so I won’t go through all that – instead I’ll write my impressions.

Okay, so Rails makes you use MVC (Model View Controller) as a design paradigm, but that’s no bad thing – I am a BIG fan of MVC. The Model part is really neat – Rails uses reflectance to define itself; that is, it looks at the database and defines the model based on the definitions it finds within. Clever, and simple.

The view – well, it does what it says. It’s quite nice – you can have templates, the views can have loops or call ‘helpers’. Works well. Plenty of functions for generation of links – and I love the automatic pagination. One line to do pagination – fantastic.

What impressed me the most was the main point of Rails, I guess – you have to do very little to get something that works up and running (which you can then develop upon), and it takes very little code. I mean, during that demo, you get a website that lists recipies, with all the create/delete/update/select actions in a total of 11 line of code, in which you wrote 1.

That’s a lot of bang per buck. I could be tempted off PHP by that.

So, the short of it – not played with Ruby itself so much, but it seems a fairly nice language. Typical one, you know. But Rails – very impressed, I can think of projects that would benefit from this. I expect it will become more popular (much more) though perhaps not amongst some of the PHP crowd, as MVC requires a certain degree of abstract though. If you’re a web developer, you probably want to take a look…

Ruby on Rails