Showing posts with label rails. Show all posts
Showing posts with label rails. Show all posts

Tuesday, January 18, 2011

Netbeans Won't Debug Rails Project

If the Netbeans says it "can't contact the web server" when running "Debug Project" there is likely an issue with the debug gems that the IDE installs itself. To correct this problem:
  1. Exit Netbeans
  2. $ gem uninstall linecache
  3. $ gem uninstall ruby-debug-base
  4. $ gem uninstall ruby-debug-ide
  5. $ gem install ruby-debug-ide
  6. Start Netbeans, and "Debug Project"

Please note that I am using MRI (native Ruby), not JRuby.
  • Netbeans 6.9.1 (Linux)
  • ruby 1.8.7 (2010-12-23 patchlevel 330) [x86_64-linux]
  • RubyGems 1.4.1
  • linecache (0.43)
  • ruby-debug-base (0.10.4)
  • ruby-debug-ide (0.4.16)

Tuesday, January 19, 2010

My .irbrc Ruby irb and Rails console configuration

Here is my customized configuration file for Ruby irb and Rails console. You will need to install the wirble and hirb gems for full functionality.

Tuesday, April 21, 2009

Rails and form_for() within Partials

This one tripped me up for almost 2 hours.

When using form_for() within a partial, use the variable reference instead of a symbol when specifying the form object. i.e. Use form_for(@user){|user_form|}, instead of form_for(:user){|user_form|}

I was creating a multi-model form with nested attributes using the new Rails 2.3 fields_for semantics, and the nested model field id's didn't have the numbered "*_attributes_0" suffix string. Ruling out syntax errors, I inspected the nested form objects and found that my user_form.object was nil, and thus the form_for method was not assigning the form object. When led me to find that my use of form_for(:user) was broken. form_for(@user) worked perfectly.

My conclusion may be incorrect, but it appears that using a symbolic reference as an object attribute for form_for() within a partial is broken.

The moral of the story is, just use the variable reference.

Saturday, March 28, 2009

Capistrano deploy.rb for Git and Phusion Passenger

Here is my Capsitrano deploy.rb script/recipe for use with Git repos on servers powered by Phusion Passenger (mod_rails).

http://gist.github.com/87207

Tuesday, October 21, 2008

Rails Application in A Subdirectory

If you want to have a Rails application work under a subdirectory, as in http://www.example.com/rails_app , then add this line to your config/environment.rb.

ActionController::AbstractRequest.relative_url_root = "/rails_app"

Thursday, July 03, 2008

Rails: Get controller route hash from url string

This is somewhat uncommon, but to get the routing hash from a URL string:

ActionController::Routing::Routes.recognize_path('/users/1')
# => {:controller => 'users', :action => 'show', :id => '1' }

Wednesday, February 27, 2008

How to Disable Asset Timestamps in Rails

By default, Rails appends a timestamp parameter to asset urls to aid in cache management. If you need to disable this, simply add the following line to your config/environment.rb

ENV['RAILS_ASSET_ID'] = ''

Saturday, February 16, 2008

Load Balance Rails Mongrel Clusters With Apache

A simple solution for load balancing a Ruby on Rails Mongrel cluster with an Apache front end is to use the randomizing feature in Apache module mod_apache’s rewrite map feature. Say, for instance, you have 3 Mongrel servers, running on ports 4000 to 4002 on localhost. First create a file map.txt containing these numbers:

ports 4000|4001|4002


Then ensure the following directives are present inside the VirtualHost stanza in your Apache configuration:

ProxyRequests Off
ProxyPassReverse / http://localhost:4000/
ProxyPassReverse / http://localhost:4001/
ProxyPassReverse / http://localhost:4002/
ProxyPreserveHost On
RewriteEngine On
RewriteMap servers rnd:/path/to/your/map.txt
RewriteRule ^/(images|stylesheets|javascripts)/?(.*) $0 [L]
RewriteRule ^/(.*)$ http://localhost:${servers:ports}/$1 [P,L]

Restart Apache, and that is it.

I have had great success using both the Pound load balancer, and Nginx, which I suggest highly, but Apache does a great job when necessary.

Wednesday, February 13, 2008

Using :db_file with attachment_fu

The README for attachment_fu says that accessing data using the :db_file backend is outside the scope of the document. Well here is my take on it. You will also need the "mimetype_fu" plugin. Follow the instructions with attachment_fu as usual, and do the following.

Assuming that your attachment model is called "Asset" add the following line to "config/routes.rb":
map.asset_data '/asset/:id', :controller => 'assets', :action => 'show'

Create an AssetController containing at least the following:
(The thumbnail of the original image is found if necessary, and mimetype_fu is used to determine the mime-type sent to the browser)
class AssetsController < ApplicationController
def show
asset = Asset.find(params[:id])
if params[:thumbnail]
data = asset.thumbnails.find_by_thumbnail(params[:thumbnail])
end
data ||= asset
send_data (data.db_file.data,
:filename => data.filename,
:type => File.mime_type?(data.filename),
:disposition => 'inline')
end
end

Add this method to app/helpers/application_helper.rb
  def assetpath(asset,thumb=nil)
"#{asset_data_url(:id => asset.id)}#{thumb.nil? ? '' : "?thumbnail=#{thumb.to_s}"}"
end


So now you may use the helper in your views (i.e.):
<%= image_tag(assetpath(asset_model_instance)) %>
<%= image_tag(assetpath(asset_model_instance,:thumb)) %>

Friday, December 14, 2007

Rails Multi-Model Forms

Here is an excellent plugin to do all the magic.

URLs With file_column

This patch modifies the Rails file_column plugin so that it may download a remote file via HTTP in addition to using uploaded data. Now you can specify a string with a full valid URL.

Run it against lib/file_column.rb

Enjoy! file_column patch.

Thursday, December 13, 2007

Rails 2.0 Screencast

He goes a bit fast:
http://www.akitaonrails.com/2007/12/10/the-first-rails-2-0-screencast-english

Direct links to the text versions:
http://www.akitaonrails.com/2007/12/12/rolling-with-rails-2-0-the-first-full-tutorial
http://www.akitaonrails.com/2007/12/12/rolling-with-rails-2-0-the-first-full-tutorial-part-2

Getting Rails 2.0 Running

So, Rails 2.0 has finally been released. It was a bit of a pain getting all my Ruby Gems updated for some reason. Please keep in mind that the MySQL path is specific to OSX 10.5.

$ sudo gem update sources
$ sudo gem update --system

$ sudo gem uninstall mysql
$ sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql
$ sudo gem update -- --with-mysql-dir=/usr/local/mysql
$ sudo gem install rails --include-dependencies

I know there is a bit of duplicated effort there, but I had issues with that MySQL gem.

Finally Rails 2.0 joy. Now to fiddle around with it before starting a new project...

Installing the MySQL Ruby Gem for Rails MySQL Support on OSX Leopard

Some special command line magic is required to install the MySQL Gem on OSX Leopard (10.5).

$ export ARCHFLAGS="-arch i386"
$ sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql


I got this information from http://trac.macosforge.org/projects/ruby/wiki/Troubleshooting.

Wednesday, November 21, 2007

Rails SQL Optimization

In order to automate the optimization of SQL queries in Ruby on Rails, I wrote this script to parse the output of the logs. It finds all the unique SQL queries made by different controllers, and runs an SQL EXPLAIN for each. This is really good for finding missing indexes and large table scans.

I have only tested this on Mac/MySQL, so your mileage may vary (it uses *NIX file paths). This script has saved me MUCH time. Of course, when optimizing queries, don't go overboard on eager loading. ActiveRecord eager loading can be slow if you use multiple nested includes.

Just run this script within your Rails application root directory, and it will create a file named explained.html. You may set the RAILS_ENV environment variable to specify production mode. The script automatically loads your database configuration from log/database.yml

Oh, and there needs to be at least 2 pageloads in your log. I haven't bothered fixing this bug.

So here is the script.

UPDATE: I came across some good tips for additional optimization here.

Thursday, January 18, 2007

Ruby Cheat Sheets

I found a handy ruby gem that makes reference very easy. gem install cheat to install the gem, then run cheat sheets to get a list of available cheat sheets. Run cheat cheat_name to display useful info.

Wednesday, January 03, 2007

Rails Development Using Vim

Rails developers fortunate enough to run OSX have raved to no end about TextMate, but what do the rest of us, being *Nix and Windows users, have available? RadRails is an EXCELLENT IDE, and I have used it almost exclusively for nearly a year now, but as an editor, like all software, it has severe limitations. It's a bit slow and too much of a memory hog for my taste, and not nearly as easy to extend as I would like.

In my experience as a Linux administrator and desktop user, I have learned to appreciate, and now love the Vi editor. Well, more accurately Vim. If you are using a GUI, Cream is an excellent set of customizations for GVim that make Vim more accessible to the new user.

To improve the state of affairs even further, there are various Ruby and Rails specific extensions and customizations that bring Vim to the point of being nearly as good, and better in some ways, than TextMate or RadRails. We're talking autocompletion, autoindention, syntax highlighting, abbreviations, and much much more. See this post (so useful to me that I mirrored it here) for how to tweak Vim for Rails development.