Posted by
admin on June 29, 2009

I've updated the
Find in Files plugin for Gedit again. It should be a tiny bit faster and also be able to search special characters. That means you can search for "<%= link_to" instead of just "%= link_to". (Characters are now properly escaped.) The conflict with the Find in Documents plugin should also work.
The original instructions for using the plugin were on my
old site, but the gist is that you (may) need SnapOpen and need to enable the default File Browser plugin. Once you enable the File in Files plugin a 3rd tab will appear at the bottom of the screen. Typing in any search results there will display any matching files under your File Browser root with matching results. Basically a glorified grep -R
At some point I may get ambitious and add the ability to exclude log files, but for the moment I'd suggest only searching within a modest scope. i.e. searching within your Rails App folder? Ok! Searching within your Rails Root folder? Not such a great idea because of the potentially large log files and also all the plugins you may have. You can try it but you've been warned..
Again, I welcome any contributions or forks, so please have at it. Gedit plugins are written in Python or C and I don't program either particularly well.


Posted by
admin on June 29, 2009

Activerecord valiations are seriously cool. If you just want to validate something in your form it's as easy as specifying the validation in your model and letting Rails take care of the rest. But what if you want to validate a form_tag? Generally, we use form_tag when there's no model associated with our form. Without the model we can't use Activerecord validations! Well, it turns out there's an elegant solution to the problem thanks to the
Tableless Activerecord gem.
Now normally I'm pretty hesitant about adding a gem dependency to my application. These things tend to break between major rails upgrades and if the plugin developer isn't beholden to thousands of users it could be a while before you see a fix. In the end, if you're desperate, you could end up maintaining code you hadn't intended to.
The Tableless Activerecord gem is pretty straight forward though. Even though it hasn't been updated in a year, it seems to work fine with Rails 2.3.2. Usage is also pretty simple. To validate a contact form without a user model, just create a file called something like contact_messenger.rb and populate with the fields in your contact form and the validates you want:
class ContactMessage < ActiveRecord::Base
has_no_table
column :name, :string
column :email, :string
column :phone_number, :string
column :message, :string
validates_presence_of :name, :email, :message
end
In the above example, our contact form has fields for name, email, phone number, and message, but we're only requiring name, email, message. All other validations will work here as well so, for example, if you want to require the phone number be of a particular length you can do that as well.
Full instructions for using the gem/plugin can be found on Kenneth Kalmer's website.
Posted by
admin on June 8, 2009

Often times I find myself using ssh to get to a server in order to get a file. The problem is that my local machine doesn't have its own publicly accessible IP address. So I end up ssh'ing into the remote server, creating the file I need (e.g. database backup) and then exiting only to run scp from my local machine afterwards. Plus, if after that I want to erase the file on the remote server I have to ssh in again and tidy up after myself. That's me running ssh x2 and scp x1 every time I want to accomplish what should be simple. There's got to be an easier way, right? You bet..
First thing you'll want to do is ssh into your server but this time we're going to pass a -R flag and a port number. That flag "Specifies that the given port on the remote (server) host is to be forwarded to the given host and port on the local side." Basically a reverse tunnel..
ssh -R 19999:localhost:22 youruser@yourserver.com
Once you're in, you can ssh back to localhost:
ssh localhost -p 19999
or copy a file down:
scp -P19999 YOUR_FILE youruser@localhost:/home/youruser/
And that is all there is to it! Credit due to HowTo Forge for pointing the way on this. They've got a bit more on the topic that you can read about here.
Posted by
admin on June 6, 2009

If you use Sphinx as your search backend you may have noticed that it won't search for an entire email address. It's a bit perplexing at first since everything else works so well. After banging my head on the issue for a few hours, I got some direct help from Pat Allan, the creator of Thinking Sphinx. The problem is with the "@" symbol which is a reserved character in Sphinx. To get around that we need to modify Sphinx's allowable character set.
Now for better or for worse, you don't actually need to define any sort of config file by default to use Thinking Sphinx.. it's smart enough to pick sensible defaults. But in this case we'll have to go ahead and create one.
Every Ruby on Rails project comes with a config folder where many files you may need to modify are stored. In this directory create a file called sphinx.yml. It's a YAML file so be careful about spacing here. It's as sensitive as your database.yml file so if you accidentally alter the spacing things may not work.
For our example, let's use the UTF-8 character set. So in that sphinx.yml file, paste this content:
development:
port: 3312
charset_table: "0..9, a..z, _, @, A..Z->a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F"
test:
port: 3313
charset_table: "0..9, a..z, _, @, A..Z->a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F"
production:
port: 3312
charset_table: "0..9, a..z, _, @, A..Z->a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F"
There are a number of other options you can put in your configuration file of course, but the charset_table one is going to be the one that helps you search email. In order to see the effect, you'll need to rebuild your index. If you've got a recent version of the Thinking Sphinx plugin:
rake thinking_sphinx:rebuild
Otherwise, you'll need to stop, index, and then start:
rake thinking_sphinx:stop
rake thinking_sphinx:index
rake thinking_sphinx:start
After that searching for emails should work!
Posted by
admin on June 3, 2009

If you've followed my
instructions for installing Ruby on Rails in Ubuntu you may have felt ambitious and also gotten Capistrano working too. The only issue is that, by default, nginx doesn't work with Capistrano's web:disable task. In order to get that working, you'll need to get your system folder setup, add a snippet of code below your server block and then restart nginx.
1. First let's add that snipped of code to nginx. If you installed nginx via phusion, then your configuration file is in /opt/nginx/conf/nginx.conf. Use any editor to add these lines within the server{} block:
if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html break;
}
2. Now that this is done, give nginx a restart:
sudo /etc/init.d/nginx stop
sudo /etc/init.d/nginx start
You can now try disabling the application using the capistrano task:
cap deploy:web:disable
3. If you get an error, that says:
Net::SFTP::StatusException (Net::SFTP::StatusException open /var/www/my_app/shared/system/maintenance.html (2, "no such file"))
Then you'll need to create a system folder. So log into your remote server and make sure that the you have a system folder with the right permissions setup.
mkdir -p /var/www/my_app/shared/system
Try it again:
cap deploy:web:disable
If it worked this time then you're done! Just don't forget to enable the server again using:
cap deploy:web:enable
Otherwise, be sure to check out the error message (most like a permission issue).
Posted by
admin on June 3, 2009

It's fairly straight forward to get indexing working using the excellent Thinking Sphinx plugin. But sometimes you may want to do something a little more complicated. For example, if you have a Model foo with a has_many relationship to model Bar and you want to search on Bar how do you set it up? Or, what if you want to find out how many Bars Foo has? Enough! Let's work on an example.
Let's start and try to index a User model. That user has many bank accounts each of which has a balance:
User 1
--> Account 1 ($5)
--> Account 2 ($100)
--> Account 3 ($3)
User 2
--> Account 4 ($5)
--> Account 5 ($75)
In our example, we want to search for users by the amount of cash they have in their accounts. To do that we need to configure sphinx to first tell it that a User has many Accounts with a bank balance:
class Brigade < ActiveRecord::Base
has_many :accounts
#sphinx index
define index do
has accounts(:bank_balance), :as => :bank_balance
indexes "SUM(bank_balance)", :as => :total_bank_balance, :sortable => true
end
end
With that done, you can now do a search for users with a bank balance of $5 and it will return the 2 users with that balance. But, you can also do a search for users with a total bank balance of $80 and it will return only user #2.