In addition to a helpful 404 page, sometimes its nice when you re-engineer an entire major site to check out the top pages from the old site and do some manual redirects in your .htaccess.
Redirect 301 /old-url http://www.domain.com/new-url
Routing for a Singleton Resource on Rails 2 (re-printed from http://www.expressionlab.com/2008/7/14/routing-for-a-singleton-resource-on-rails-2)
While developing your RESTful Rails application, you use ActionController::Resources which magically generates a bunch of named routes to use in controllers and views.
For example, adding the following line to your config/routes.rb:
map.resources :products
gives you these paths: products_path, product_path(id), new_product_path and edit_product_path(id).
If your resource is a singleton one, you should simply do this:
map.resource :store
However, when you try to navigate to the edit action (/store/edit) you will get an error like this:
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.to_sym
from the line that says in your view.
I’ve discovered that there is already an open ticket for this issue. The problem is that Rails still assumes a collection resource. To fix this, you need to explicitly add the path to the form_for helper:
<% form_for(@store, :url => store_path) do |f| %>
Also, you will need to do the same in your update action:
redirect_to(store_path)
Now, if you are like me, you may try to use a singular name for your controller (StoreController). Now, you should get the next error:
uninitialized constant StoresController
Who mentioned StoresController?!
Well, Rails did! As documented here, the default controller name is still taken from the plural name.
To solve this, just add the controller name to your route:
map.resource :store, :controller => 'store'
Now, everything should work fine.
By the way, I have found a patch that allows scaffold generator to produce singleton resources: http://dev.rubyonrails.org/ticket/8832. But I didn’t try it yet. Anyone did?
Installing PHP APC on GNU/Linux Ubuntu Gutsy Gibbon 7.10 (and Debian) (re-printed from http://2bits.com/articles/installing-php-apc-gnulinux-ubuntu-gutsy-gibbon-710-and-debian.html
Complex PHP applications, such as Drupal, can gain a lot of performance benefits from running a PHP op-code cache/accelerators.
APC, Alternate PHP Cache, is now the most maintained free/open source op-code cache, and is being used more and more as it emerges to be the most stable.
The instructions here detail how to get APC running on an Ubuntu server running Gutsy Gibbon 7.10.
First, we need the pecl command so we can download and install APC from the repositories.
Do to so, we execute the following command:
aptitude install php-pear
But, this will not run on its own, we need the following package for the phpize command:
aptitude install php5-dev
We also need the apxs command, which is installed via the following package:
aptitude install apache2-dev
Now we have all the software we need, so we install apc via the pecl command:
pecl install apc
Once that finishes, we need to enable apc in Apache’s configuration. the following command should do this for us.
echo "extension=apc.so" > /etc/php5/apache2/conf.d/apc.ini
Then we restart Apache:
/etc/init.d/apache2 restart
And we are all done. Watch for less execution time per page, and decreased memory usage per Apache process compared to what you had before.
Don’t you hate when you cut your lip on plastic utensils?
Best non-played out joke ever: Hold an iPad up to your face and go “HELLO?”
If your ajax call is looking for json back…
$.ajax({
type: "POST",
dataType='json',
url: url,
success: function(data, textStatus) {
console.log('Throw a party');
}
});
…but it gets nothing back but a blank string…
def party_hard
respond_to do |format|
format.html { redirect_to '/feed', :status => 400 }
format.js { head :ok }
end
end
the success function will never run. Use dataType script instead.
When trying to make a jQuery ajax request, don’t get JSON happy and set everything to JSON. Read the documentation instead.
contentType - When sending data to the server, use this content-type.
dataType - The type of data that you’re expecting back from the server.
If you set contentType to application/json and try to make ajax POST requests with a serialized form, crazy stuff happens. If you don’t set a dataType and try to hit a controller that responds to both html and js, you’ll get the html.
respond_to do |format|
format.html
format.js { render :json => @health_reform.to_json }
end
If you have a link that you’ve unobtrusively turn into an ajax request on click and your browser is downloading a crazy text file when you click the link, you forgot the “return false” at the end if your click event.
map.connect ‘justin_bieber/:age’, :controller => ‘justin_bieber’, :action => ‘show’ will catch ‘http://localhost:3000/justin_biebers’ and throw you an age not found error. Be sure to put whatever route handles the index action above the show action if you aren’t using resources.
Create Database create database microsoftaccess;
Create User create user ‘chairthrower’@’localhost’ identified by ‘welcome1’;
Grant Permissions grant all on microsoftaccess.* to ‘chairthrower’@’localhost’;
Export mysqldump -u root -p microsoftaccess > microsoftaccess.sql
Import mysql -u root -p microsoftaccess < microsoftaccess.sql
When you create an ActiveRecord Model and save it to the database (Model.create), Rails stores the current time in UTC, not your current time zone. When you retrieve the record (Model.find), Rails converts the time from UTC back into your own time zone.
Problems arrive when you try to find your records by a specific time:
Model.find(:all, :conditions => ["created_at < ?", Time.now]
or when trying to perform an action that skips ActiveRecord:
Model.update_all(["updated_at = ?", Time.now], "id = 1")
You have to use Time.zone.now to get the time in the right time zone to send to ActiveRecord. This link might explain it better: http://www.idolhands.com/ruby-on-rails/guides-tips-and-tutorials/activerecord-find-conditions-and-time-zone-suppor
Ever get really confused by the same thing for 10 minutes before realizing “OH I’VE DONE THIS 100 TIMES BEFORE!!!” I hope this note to self will fix it for next time.
Note to self: Before you waste 10 minutes figuring out why a flash[:notice] either a) isn’t working or b) is sticking around for one refresh too long, remember that flash.now works only for the current action and flash[:notice] = ‘forward’ is available for the next action. You know, like a redirect.

