I've been customizing my gnome panels, and I happened to mess them up pretty bad. However, I found an awesome way to reset the gnome panels back to the default here.
gconftool --recursive-unset /apps/panel
rm -rf ~/.gconf/apps/panel
pkill gnome-panel
W
Showing posts with label Solutions. Show all posts
Showing posts with label Solutions. Show all posts
Tuesday, December 23, 2008
Thursday, October 23, 2008
Named Routes: Anchor
If you're using named routes and want to add an anchor to your automatically generated URL, there's a very simple way to do it:
products_path(:anchor => 'my_anchor')
It's that simple!
W
products_path(:anchor => 'my_anchor')
It's that simple!
W
Sunday, October 19, 2008
Textarea: Preserving Whitespace
A lot of the times when you let users post things using textareas, it becomes frustrating to lose whitespace when you display their posts (especially carriage returns).
Well, enter <pre>. This nifty little tag preserves any whitespace within the tags. However, there is one caveat. It preserves whitespace so well that it doesn't know how to word wrap. This is troublesome when your user continues to type in the textarea without hitting the carriage return. When displayed with the pre tags, the words are all on one long line.
Well, here's a nice little hack:
http://www.longren.org/2006/09/27/wrapping-text-inside-pre-tags/
W
Well, enter <pre>. This nifty little tag preserves any whitespace within the tags. However, there is one caveat. It preserves whitespace so well that it doesn't know how to word wrap. This is troublesome when your user continues to type in the textarea without hitting the carriage return. When displayed with the pre tags, the words are all on one long line.
Well, here's a nice little hack:
http://www.longren.org/2006/09/27/wrapping-text-inside-pre-tags/
W
Friday, October 17, 2008
VMware Server: Adding a USB
Ever wondered how to add USB capability to your VMware server? Well, I had a little bit of a problem trying to get my USB connected.
I had gone to the settings for my VM and added the USB controller. Then, when I booted up my VM, I went to
VM > Removable Devices > USB Devices > ...
To my surpise, it was empty! I knew my device was plugged in because my Ubuntu detected it, but I couldn't see it. After searching around, I found a solution:
1. power off your VM
2. unplug your USB
3. >sudo gvim /etc/init.d/mountdevsubfs.sh
4. uncomment lines 42-45 (starts off with #mkdir -p /dev/bus/usb/.usbfs...)
5. >sudo /etc/init.d/mountdevsubfs.sh start
6. start your VM
7. plug in your USB
8. add your USB to the VM
Good luck!
W
I had gone to the settings for my VM and added the USB controller. Then, when I booted up my VM, I went to
VM > Removable Devices > USB Devices > ...
To my surpise, it was empty! I knew my device was plugged in because my Ubuntu detected it, but I couldn't see it. After searching around, I found a solution:
1. power off your VM
2. unplug your USB
3. >sudo gvim /etc/init.d/mountdevsubfs.sh
4. uncomment lines 42-45 (starts off with #mkdir -p /dev/bus/usb/.usbfs...)
5. >sudo /etc/init.d/mountdevsubfs.sh start
6. start your VM
7. plug in your USB
8. add your USB to the VM
Good luck!
W
Sunday, September 28, 2008
RoR: attr_accessible
In a previous post, I talked about mass assignment. Well, there's something I discovered recently. If you have a polymorphic relationship, your model will have two columns: taggable_type and taggable_id. If you want to make these available for mass assignment, you cannot do:
attr_accessible taggable_type, taggable_id
Instead, RoR treats them like one attribute like so:
attr_accessible taggable
W
attr_accessible taggable_type, taggable_id
Instead, RoR treats them like one attribute like so:
attr_accessible taggable
W
Saturday, September 27, 2008
RoR Generate + SVN
We all know about RoR's ability to generate controllers, models, migrations, etc. However, if you're using a repository, you have to manually add these newly created files. This could be a pain in the ass!
Here's a tip. If you're using SVN, you'll love this. Simply add the flag --svn and the generator will automatically add the newly created files to SVN. Simple!
W
Here's a tip. If you're using SVN, you'll love this. Simply add the flag --svn and the generator will automatically add the newly created files to SVN. Simple!
W
Thursday, September 25, 2008
RoR: Mass Assignment Security
In RoR, it is possible to do something called mass assignments. For instance, when submitting a form, all the parameters are put into a params hash so you can do the following:
Product.create(params[:product])
product.update_attributes(params[:product])
Very simple, but by default, there is a security flaw. That flaw is covered in this post. If you are concerned about this, the easiest way would be to make it so that all your models will require attr_accessible on any attributes that you want mass assignment for. This then protects any attributes not listed by default.
In your config files, preferably in your initializers folder, add the following to protect all attributes:
ActiveRecord::Base.send(:attr_accessible, nil)
Then go through each model and think about which attributes should be exposed to mass assignments. For those that are not accessible, you must do the following to assign values:
product.store_id = store.id
This makes the assignment explicit and cannot be manipulated by the users.
Now that you know, go secure your applications!
W
Product.create(params[:product])
product.update_attributes(params[:product])
Very simple, but by default, there is a security flaw. That flaw is covered in this post. If you are concerned about this, the easiest way would be to make it so that all your models will require attr_accessible on any attributes that you want mass assignment for. This then protects any attributes not listed by default.
In your config files, preferably in your initializers folder, add the following to protect all attributes:
ActiveRecord::Base.send(:attr_accessible, nil)
Then go through each model and think about which attributes should be exposed to mass assignments. For those that are not accessible, you must do the following to assign values:
product.store_id = store.id
This makes the assignment explicit and cannot be manipulated by the users.
Now that you know, go secure your applications!
W
Friday, September 19, 2008
Prototype: $A is not defined
Ever get this annoying error? Well... I do. Odd thing is, everything still works like a charm. So what's happening.
Unfortunately, no one seems to really know! I did some digging and found this bug. If you go through the comments, Prototype has some problems when loading/unloading a page on Firefox 2. Or maybe it's Firebug. People can't seem to entirely agree, and the bug has been reopened several times.
However, if you want a patch and forget about finding out what the problem really is, check out this fix.
I've used it and forgot about it now. Would be nice to know why though...
W
Unfortunately, no one seems to really know! I did some digging and found this bug. If you go through the comments, Prototype has some problems when loading/unloading a page on Firefox 2. Or maybe it's Firebug. People can't seem to entirely agree, and the bug has been reopened several times.
However, if you want a patch and forget about finding out what the problem really is, check out this fix.
I've used it and forgot about it now. Would be nice to know why though...
W
Wednesday, September 17, 2008
Database Visualization
Keeping documentation up-to-date is always a pain in the ass. I was recently working on updating our database diagrams and thought that there may be a way to automate the process.
I found 2 solutions:
1. XMI
2. RailRoad
For solution (1), it worked, but I had to tweak it slightly. Because we are using PostgreSQL, I had a problem when UmlDumper tried to execute current_database for the adapter. This works fine for the MySQL adapter, but no such function exists for the PostgreSQL adapter. I quickly hacked it and just replaced it with a general name for our database, as we didn't have to be dead-on here.
I was quite disappointed when Dia did not know how to open up the XMI file. So, searching around, I found a program called Umbrello. It works great. Unfortunately, I had all my UML classes, but no diagram! I had to drag and drop each one individually (I couldn't figure out how to drag them all at the same time T.T). I then manually added in all the lines and made it pretty. Not too bad, but it was partly automated and partly manual.
For solution (2), it worked great. I used a rake task to generate the svg files. I had several issues with this, which was the fact that RailRoad is used to diagram models in RoR, not the database. Hence, I ended up with a lot of relations that didn't exist in the database. This meant TONS of lines. The lines are also quite hard to follow as they constantly overlap and cross each other.
2 major issues I had was that there is a 1-to-1 relationship (for efficiency purposes). However, the diagram did not reflect this. For example, we have class A and B that are in a 1-to-1 relationship. A will have has_many relationships and so will B. However, the diagram showed that all of B's has_many relationships were related to A, not B. I couldn't figure out why.
The other issue was a logged bug. If the model ends in an 's', the diagram gets messed up. The diagram will automatically drop the trailing 's'. This created incorrect relationships.
For these 2 major issues, I was unable to use solution (2) in the end. However, if more improvements are made, I may give it another chance as the whole process was completely automated. If your interested, it also does Controller diagrams.
If anyone else has a better way of producing automated diagrams of their database or models, give me a shout. I'd love to hear other solutions.
W
I found 2 solutions:
1. XMI
2. RailRoad
For solution (1), it worked, but I had to tweak it slightly. Because we are using PostgreSQL, I had a problem when UmlDumper tried to execute current_database for the adapter. This works fine for the MySQL adapter, but no such function exists for the PostgreSQL adapter. I quickly hacked it and just replaced it with a general name for our database, as we didn't have to be dead-on here.
I was quite disappointed when Dia did not know how to open up the XMI file. So, searching around, I found a program called Umbrello. It works great. Unfortunately, I had all my UML classes, but no diagram! I had to drag and drop each one individually (I couldn't figure out how to drag them all at the same time T.T). I then manually added in all the lines and made it pretty. Not too bad, but it was partly automated and partly manual.
For solution (2), it worked great. I used a rake task to generate the svg files. I had several issues with this, which was the fact that RailRoad is used to diagram models in RoR, not the database. Hence, I ended up with a lot of relations that didn't exist in the database. This meant TONS of lines. The lines are also quite hard to follow as they constantly overlap and cross each other.
2 major issues I had was that there is a 1-to-1 relationship (for efficiency purposes). However, the diagram did not reflect this. For example, we have class A and B that are in a 1-to-1 relationship. A will have has_many relationships and so will B. However, the diagram showed that all of B's has_many relationships were related to A, not B. I couldn't figure out why.
The other issue was a logged bug. If the model ends in an 's', the diagram gets messed up. The diagram will automatically drop the trailing 's'. This created incorrect relationships.
For these 2 major issues, I was unable to use solution (2) in the end. However, if more improvements are made, I may give it another chance as the whole process was completely automated. If your interested, it also does Controller diagrams.
If anyone else has a better way of producing automated diagrams of their database or models, give me a shout. I'd love to hear other solutions.
W
Labels:
ActiveRecord,
Controllers,
Database,
Solutions,
W
Friday, September 12, 2008
Ubuntu: Volume Problems
Recently, my audio has been much softer than it has been when I first installed Ubuntu. Even if I turn my volume up completely, the audio was only at a normal volume. I found this odd because before, when I turned it up, it would blast my ears off.
I managed to find out why! If you go to your Volume Control, Edit > Preferences, and select PCM. Then turn the PCM volume up to the desired level. Mine, for some reason, was set at a very low volume.
I'm not quite sure what happened, as I never played with my settings. My only guess is that some particular update fooled with the settings.
W
I managed to find out why! If you go to your Volume Control, Edit > Preferences, and select PCM. Then turn the PCM volume up to the desired level. Mine, for some reason, was set at a very low volume.
I'm not quite sure what happened, as I never played with my settings. My only guess is that some particular update fooled with the settings.
W
Tuesday, September 9, 2008
BackgrounDrb.destroy
So we've been noticing some pretty odd things with BackgrounDrb lately. In spite of our previous fixes to BackgrounDrb, we've decided to give up on it. Why? Simply put: It's not working properly.
We've been checking the logs recently, and we've noticed way too many calls to our scheduled tasks. On the odd days, we'll get 3 or 4 extra calls when only 1 was scheduled. I don't know how to explain this, nor do I wish to find out, because frankly, I'm fed up with this.
I admit, it really was my fault for trying to use an overly complicated program to run something very simple. KISS - I know. I'm kicking myself in the ass for this now! Anyways, simple solution: cron. It's the most basic of basic schedulers. You can't go wrong here.
Luckily, all our functions were encapsulated in the models, so the logic didn't need to be changed. We ripped out the stuff in the BackgrounDrb worker and placed them in a rake file. We then called the rake tasks from the cron.
Very simple. Very clean.
Bye Bye BackgrounDrb!
W
We've been checking the logs recently, and we've noticed way too many calls to our scheduled tasks. On the odd days, we'll get 3 or 4 extra calls when only 1 was scheduled. I don't know how to explain this, nor do I wish to find out, because frankly, I'm fed up with this.
I admit, it really was my fault for trying to use an overly complicated program to run something very simple. KISS - I know. I'm kicking myself in the ass for this now! Anyways, simple solution: cron. It's the most basic of basic schedulers. You can't go wrong here.
Luckily, all our functions were encapsulated in the models, so the logic didn't need to be changed. We ripped out the stuff in the BackgrounDrb worker and placed them in a rake file. We then called the rake tasks from the cron.
Very simple. Very clean.
Bye Bye BackgrounDrb!
W
Tuesday, September 2, 2008
Sitemap
It's nice to have a sitemap that adheres to the Sitemap 0.90 protocol. In rails, this can be automatically generated. Unfortunately, you have to do some work.
I found 2 different ways:
1. using controllers
2. using rake
If your site is small, (1) works really well. It's as dynamic as it can be as the sitemap is generated on the fly. However, every time anyone visits that URL, you end up hitting the database for a fairly large call, memory-wise. If it's just legitimate people accessing it, they will access the URL when they need it, or on a periodic time frame. However, if you encounter malicious users (which there are many out there), they could easily overload your server.
(2) is not as up-to-date, but from my point of view, that's ok, especially if you update the files daily. The solution posted works well, but since out site takes advantage of BackgrounDrb, I thought I would modify it to work with BackgrounDrb instead of rake.
I created some self functions to do the database queries and handle the tasks in the rake. Also, in sitemap.rb, I added the lines:
include ActionController::UrlWriter
default_url_options[:host] = 'www.twoblindfish.com'
This allows you to use named routes and url_for. Now we don't need to hardcode our URLs! Another plus! The rest is pretty straight forward. A few of the tasks were redundant, so I refactored them into multiple functions for reuse.
The only thing really different would be to place your sitemaps in public/system/. so that the sitemaps don't get removed when you do a cap:deploy. This works because the public/system folder is symlinked to shared/system.
Hope this helps those in need of generating sitemaps!
W
I found 2 different ways:
1. using controllers
2. using rake
If your site is small, (1) works really well. It's as dynamic as it can be as the sitemap is generated on the fly. However, every time anyone visits that URL, you end up hitting the database for a fairly large call, memory-wise. If it's just legitimate people accessing it, they will access the URL when they need it, or on a periodic time frame. However, if you encounter malicious users (which there are many out there), they could easily overload your server.
(2) is not as up-to-date, but from my point of view, that's ok, especially if you update the files daily. The solution posted works well, but since out site takes advantage of BackgrounDrb, I thought I would modify it to work with BackgrounDrb instead of rake.
I created some self functions to do the database queries and handle the tasks in the rake. Also, in sitemap.rb, I added the lines:
include ActionController::UrlWriter
default_url_options[:host] = 'www.twoblindfish.com'
This allows you to use named routes and url_for. Now we don't need to hardcode our URLs! Another plus! The rest is pretty straight forward. A few of the tasks were redundant, so I refactored them into multiple functions for reuse.
The only thing really different would be to place your sitemaps in public/system/. so that the sitemaps don't get removed when you do a cap:deploy. This works because the public/system folder is symlinked to shared/system.
Hope this helps those in need of generating sitemaps!
W
Tuesday, August 26, 2008
Geokit: Oddities
1. When using Geokit + Google, sometimes, giving a valid postal code will fail. It's a very random thing. I'm not sure if it's just Google, but sometimes, it fails, even with a valid postal code. I'm guessing it has something to do with the network or perhaps they limit how many you can ask for in a period of time so you don't overload their server.
2. If you add :within to your find, Geokit doesn't recognize that :within => nil means not to add :within. Sometimes, you have a variable that may or may not be nil, and this controls whether you want to search using a distance. The :origin key works fine with nil, but :within does not. It blindly adds the distance calculations to the find. I had to hack acts_as_mappable.rb a little bit in order to get it to work.
In the apply_distance_scope(options) function, I have:
distance_condition = "#{distance_column_name} <= #{options[:within]}" if options.has_key?(:within) && !options[:within].nil?
distance_condition = "#{distance_column_name} > #{options[:beyond]}" if options.has_key?(:beyond) && !options[:beyond].nil?
and
[:within, :beyond, :range].each { |option| options.delete(option) }
This will take the :within => nil into account and skip the adding of the distance calculations out of the find and remove the useless keys.
3. In the Geokit README, there is a section about using :includes. However, there isn't a section on using :joins. Apparently, when you use :joins, things can screw up. For instance, if I have
Shop.find(:joins => "INNER JOIN products where products.shop_id = shops.id", :conditions => ..., :origin => ..., :within => ...)
You would expect a Shop object to be returned. This is true, but I found a small error. The Product id replaced the Shop id, leaving me with a Shop object with an id of the Product id.
I traced this down to the function add_distance_to_select(...). In this function, it blindly sets the :select option in the find to "*" if it isn't already defined. I'm guessing, because both Shop and Product have ids, they get mixed up somehow.
In order to fix this, I needed to add :select => "shops.*".
Hopefully, this info helps those that are having the same problems. If you have a better solution, feel free to share!
W
2. If you add :within to your find, Geokit doesn't recognize that :within => nil means not to add :within. Sometimes, you have a variable that may or may not be nil, and this controls whether you want to search using a distance. The :origin key works fine with nil, but :within does not. It blindly adds the distance calculations to the find. I had to hack acts_as_mappable.rb a little bit in order to get it to work.
In the apply_distance_scope(options) function, I have:
distance_condition = "#{distance_column_name} <= #{options[:within]}" if options.has_key?(:within) && !options[:within].nil?
distance_condition = "#{distance_column_name} > #{options[:beyond]}" if options.has_key?(:beyond) && !options[:beyond].nil?
and
[:within, :beyond, :range].each { |option| options.delete(option) }
This will take the :within => nil into account and skip the adding of the distance calculations out of the find and remove the useless keys.
3. In the Geokit README, there is a section about using :includes. However, there isn't a section on using :joins. Apparently, when you use :joins, things can screw up. For instance, if I have
Shop.find(:joins => "INNER JOIN products where products.shop_id = shops.id", :conditions => ..., :origin => ..., :within => ...)
You would expect a Shop object to be returned. This is true, but I found a small error. The Product id replaced the Shop id, leaving me with a Shop object with an id of the Product id.
I traced this down to the function add_distance_to_select(...). In this function, it blindly sets the :select option in the find to "*" if it isn't already defined. I'm guessing, because both Shop and Product have ids, they get mixed up somehow.
In order to fix this, I needed to add :select => "shops.*".
Hopefully, this info helps those that are having the same problems. If you have a better solution, feel free to share!
W
Tuesday, July 15, 2008
Gentoo + Vim
To my dismay, when I was using Gentoo, Vim was not installed. Sad as I was, I attempted to rectify this problem. No worries... a simple 1-liner:
emerge -av vim
and I'm happy once again ^^
W
emerge -av vim
and I'm happy once again ^^
W
Tuesday, July 1, 2008
VMWare Server Fix
I managed to find a solution to my previous problem after updating my Ubuntu. It's quick and it also retains all the states of the virtual machines ^^
https://answers.launchpad.net/ubuntu/+question/36849
And just in case the link ever dies:
sudo apt-get install vmware-server-kernel-source
cd <temp dir>
tar xvjf /usr/src/vmware-server.tar.bz2
cd modules/vmware-server-kernel/
export KPKG_DEST_DIR=<where the deb package should go> # otherwise fakeroot won't work
export KVERS=`uname -r`
fakeroot debian/rules binary-modules
sudo dpkg -i $KPKG_DEST_DIR/vmware-server-kernel-modules-`uname -r`*.deb
cd -
rm -r modules
sudo /etc/init.d/vmware-server restart
Enjoy!
W
https://answers.launchpad.net/ubuntu/+question/36849
And just in case the link ever dies:
sudo apt-get install vmware-server-kernel-source
cd <temp dir>
tar xvjf /usr/src/vmware-server.tar.bz2
cd modules/vmware-server-kernel/
export KPKG_DEST_DIR=<where the deb package should go> # otherwise fakeroot won't work
export KVERS=`uname -r`
fakeroot debian/rules binary-modules
sudo dpkg -i $KPKG_DEST_DIR/vmware-server-kernel-modules-`uname -r`*.deb
cd -
rm -r modules
sudo /etc/init.d/vmware-server restart
Enjoy!
W
Saturday, June 7, 2008
OpenOffice Spellchecker
OpenOffice is a little biased when including language dictionaries for spellchecking. For instance, the Canadian English dictionary is not included by default. However, this can easily be remedied by doing the following:
1. File > Wizard > Install new dictionaries...
2. follow the wizard and install the appropriate languages you want
3. Close all OpenOffice programs
4. Restart OpenOffice
Now, when you go to:
Tools > Options... > Language Settings > Languages > Default languages for documents > Western
you should be able to select "English (Canada)" and it should have an "ABC" with a checkmark icon. This means you can now spellcheck in Canadian English!
W
1. File > Wizard > Install new dictionaries...
2. follow the wizard and install the appropriate languages you want
3. Close all OpenOffice programs
4. Restart OpenOffice
Now, when you go to:
Tools > Options... > Language Settings > Languages > Default languages for documents > Western
you should be able to select "English (Canada)" and it should have an "ABC" with a checkmark icon. This means you can now spellcheck in Canadian English!
W
Tuesday, May 27, 2008
Comma Separated Numbers
I found a neat little function that will separate large numbers by thousands using a comma or any specified delimiter:
http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#M000997
Gotta love RoR... they simplify everything!
W
http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#M000997
Gotta love RoR... they simplify everything!
W
Monday, May 26, 2008
Ubuntu: .docx files
OpenOffice is amazing when it comes to opening up .doc files created from Microsoft Word. However, there's a slight problem when it comes to opening .docx. Simply put, OpenOffice doesn't recognize it!
Don't worry though... there's always a solution! Check this out and follow the instructions posted by stuporglue.
Enjoy!
W
Don't worry though... there's always a solution! Check this out and follow the instructions posted by stuporglue.
Enjoy!
W
Saturday, May 17, 2008
Time Ago
Ever wanted to show the user that something happened so many minutes/hours/days/etc ago? Apparently, RoR makes it super simple to do this. There is a method called:
time_ago_in_words
Simply include the time (from now) that you want to calculate - perhaps a created_at/updated_at field.
Easy!
W
time_ago_in_words
Simply include the time (from now) that you want to calculate - perhaps a created_at/updated_at field.
Easy!
W
Wednesday, May 14, 2008
Alternating Table Row Colours
When listing data out in a table, it's always nice to have alternating row colours. This makes it infinitely easier to scan the table and much more enjoyable.
RoR makes it really simple to alternate row colours. A cycle function is given to aid in this task, and it simply cycles between the values given. By using this, you can specify your alternating class names here.
If you have more than one table, remember to call the reset_cycle function to reset the cycle.
Easy!
W
RoR makes it really simple to alternate row colours. A cycle function is given to aid in this task, and it simply cycles between the values given. By using this, you can specify your alternating class names here.
If you have more than one table, remember to call the reset_cycle function to reset the cycle.
Easy!
W
Subscribe to:
Posts (Atom)