Changing the name of an app’s cookie in Rails
While running Tracks and another app on my localhost, I realized that every time I did something on one, it would log me out of the other. Overwriting each other’s cookies!
Add this line to your config/environment.rb:
ActionController::Base.session_options[:session_key] = ‘yourappnamehere_session_id’
Then restart the app. All better now.
Using MySQL for storing Rails sessions
$ rake db:sessions:create
$ rake db:migrate
Possibly use -c on the first command to add the migration file to SVN?
Uncomment this line in your config/environments.rb:
config.action_controller.session_store = :active_record_store
Then restart your app.
Doing this is supposed to be more better than the default of using the file system.
Rails on DreamHost
Follow instructions carefully: Yet Another Guide to Installing Mephisto on Dreamhost
I had some trouble getting this to work, but I think it ended up being my own fault for not following the instructions to the letter. I’m going to have to try installing another app and see if it works again.
Note: when this guide mentions gem_name, don’t include .gem in the command. That is all.
Design Patterns
I wrote these terms down a long time ago while talking with JJ about programming.
- DAO
- Data Access Object
- VO
- Value Object
- MVC
- Model View Controller
Basic CRUD for Nested Elements using RESTful Rails
Continuing from where we left off…
(This all assumes you’ve set your app up using script/generate scaffold_resource with all the bells and whistles of spelling out your table columns in the command line.)
Make sure you’ve got your model relationships set up right:
# app/models/manufacturer.rb
has_many :appliances
# app/models/appliance.rb
belongs_to :manufacturer
Set up your nested routes:
map.resources :manufacturers do |manufacturers|
manufacturers.resources :appliances
end
In the controller for the dependent item, at the top of the file:
# app/controllers/appliances_controller.rb
before_filter :load_manufacturers
Then at the bottom of the same file:
private
def load_manufacturers
@manufacturer = manufacturer.find(params[:manufacturer_id])
end
Then back at the top of that file, for the index action:
@appliances = @manufacturer.appliances.find(:all)
Now for the views. To get started: in app/views/appliances/index.rhtml you should make sure that calls to appliance_path get the manufacturer id passed to them:
<%= link_to 'Show', appliance_path(@manufacturer, appliance) %>
In edit.rhtml and new.rhtml (or your _form.rhtml partial), you can remove the part of the form that lets you specify what manufacturer you want within the new/edit appliance form. You’ll get that from the params[:manufacturer_id] anyway. In the edit template, you’ll need to change the form a little bit for it to work:
# app/views/appliances/edit.rhtml
<% form_for(:appliance, :url => appliance_path(@manufacturer, @appliance), :html => { :method => :put }) do |f| %>
Notice we have to specifically specify the @manufacturer. Now back to the appliances controller:
def create
@appliance = appliance.new(params[:appliance])
@appliance.manufacturer_id = params[:manufacturer_id]
respond_to do |format|
if @appliance.save
flash[:notice] = ‘Appliance was successfully created.’
format.html { redirect_to appliance_url(@appliance.manufacturer_id, @appliance) }
…
It’s not smart enough to know that it needs the manufacturer_id param that’s just sitting there in the URL waiting to be used for something. Just about anywhere you need the appliance_url(), you have to specify the manufacturer_id. Whether it’s a param, or if it’s already sitting in the item you’re using:
# app/controllers/appliances_controller.rb
def update
...
if @appliance.update_attributes(params[:appliance])
format.html { redirect_to appliance_url(@appliance.manufacturer_id, @appliance) }
…
Hope that helps. It’s simple, right? ;)
Thanks again to PeepCode!
Spellcheck input boxes in Firefox 2.0
In case you were unaware, you can also enable spell checking in text inputs (the smaller, thinner inputs where you enter text like the search input on google.com) but it’s a little tricky: in your location bar enter about:config, scroll down to layout.spellcheck:Default, double click that line, change the 1 to a 2, and you’re done!
Deleting changes made to default bundles in TextMate
From the Help file:
If you want to discard local changes then currently the only option is to delete these from ~/Library/Application Support/TextMate/Bundles.
Of course, you can also just drag them out of that folder in case you want to add them back later. I created a folder called DeltedChanges within ~/Library/Application Support/TextMate/Bundles.