ruby on rails country_options_for_select helper
So I’m still working away on this project using Ruby on Rails, and generally I’m loving it. It really is a great piece of software. Progress is good, but I often find myself stumbling on what should be the simple things.
Today for example, I wanted to use the country_options_for_select() form helper to generate a list of countries as a dropdown in a form, with a number of countries prioritised at the top of the list.
The list was being generated but the prioritised countries where not be generated. The code I had was something like this:country_options_for_select(priority_countries = ['United Kingdom','United States']).
Now the method signature in the api docs is this: country_options_for_select(selected = nil, priority_countries = nil), and I assumed that the selected = nil didn’t need to be included as it was the default.
Not so. Once I added the selected = nil argument to the method it worked.
And the lesson is?
Assumption is the mother of all feck ups I guess.
Formatting dates in rails templates using strftime()
Recently I needed to be able to format a date that I had retrieved from the database into something more readable. In smarty one would do something like $date|date_format but I could not find anything like that in ruby.
Then someone pointed out that the date field in the database would be recognised as such and would be a date object in my View.
As the date is a Date object I can use strftime() to reformat the date any way I want to.
eg. @event.start_date.strftime("%d %B %Y")
How to find the id of a record you have just created in ruby on rails?
How do you get the id of a record you have just created in Ruby on Rails?
Well, the answer to this question came up on the rails mail list recently.
It goes something a little bit like this:
@text.save //insert something into the database
@text.id //get the id of the new record
stop gnome opening new windows
I’m currently playing with fedora core 4 on my desktop in the office. One of my initial niggles is the way that the file browser app (nautilus) always opens new windows, i.e. when you click on a folder and then click on a sub folder, one ends up with two seperate windows. Very annoying.
Anyway, there is a way to stop this by telling nautilus to work in ‘browser’ mode which is much more like the windows explorer style.
Instructions on how to change this setting are here. Note, one gets to ‘gconf’ by typing gconf-editor from a console window, or by navigating to Applications | System Tools | Configuration Editor (at least on my default gnome setup).
Cannot send session cookie ....
While working on a site last night I got this error when I uploaded the latest version of my code to the live server: Warning: session_start(): Cannot send session cookie – headers already sent …
Now, my first reaction was WTF! as I wasn’t seeing this on my development box. I was using the Apache Friends XAMPP product to run a WAMP install on my local windows XP machine so I guess there are some differences in the way apache works on win32 and linux.
Anyway, the Cannot send session cookie error is usually caused by outputting something to the client and then trying to send a cookie/write to the headers. So I start looking through my included files, trying to find a stray debug line or something like that. Nothing.
Ok, its late and no one will probably notice, but I have to find the problem.
Bang head on table a few times.
Ok, start again.
And then I see it. I had tabbed all of the source in one of my include files. Including the opening < ?php line! So, I was effectively sending a tab to output then processing my php.
More head slapping. General relief etc.
So there you go. If you ever have that problem and can’t see where its coming from, save yourself some wear and tear on the ole forehead and check for whitespace at the beginning of your source files.
Beware register_globals
We deployed a new site last week. Over the weekend I got a bug report from the site owner. Within the administration section of the site a certain session variable was getting reset causing a certain operation to fail.
Initially I was stumped. It worked fine on the dev server... So I made sure that we had deployed the code to the live server correctly. Yup, all fine.
Versions of php were the same.
Well, to cut a long story short, register globals had been turned on in an apache config file for the previous version of the site and hadn't been removed when the new site was deployed.
This was causing a variable initialisation in one part of the code to overwrite an object in $_SESSION elsewhere.
Weird. Lesson learned...
How to properly turn off comments
I was still having problems with getting sh*t loads of spam via the old posts on this site, even though I had turned off all commenting in the admin as far as I could tell. Seems I needed to sort out some settings on the old posts. Info on this can be found in this post on the wordpress site.
Specifically:
UPDATE wp_posts SET comment_status = ‘closed’, ping_status = ‘closed’ WHERE comment_status = ‘open’;
Ahhh, peace at last.
Update: and now I can gleefully look at all those feeble attempts to spam me in my logs. :)
turning off comments
I’m getting more comment spam on this blog than email spam to the half dozen or so domains I have registered so I’m just going to turn off comments for now. It’s really annoying but spam just really gets on my nerves, and I really don’t like the idea of these spammers burning up my bandwidth or more importantly my time.
If anyone wants to get in touch with me about this blog or anything else the follow the 'about me' link top right to my main site where there is a contact form...
nesting variables in smarty
Ever need to use nested variables in smarty? I know its not very mvc, but on more than one occassion I’ve found myself needing to . So here is how to do it, as a reminder to myself more than anything tbh.
{{$Availability[$product.stockLevel]}}
Improve Spam detection in Thunderbird mail client
I’ve noticed recently that the junk mail filtering in the Thunderbird mail client wasn’t as good as I would have liked. So I had a hunt around and found this thread on the MozillaZine forums with a good tip to improve its spam detection rules. Warning, involved some hand editing of preference files…