Clearing Memcached at Capistrano Deployment

Capistrano, Ruby on Rails No Comments »

It’s a two step process:

1. Create a rake task (in lib/tasks/clear_memcached.rake for example):

namespace :cache do
  desc ‘Clear memcache’
  task :clear => :environment do
    ActionController::Base.cache_store.clear
  end
end

2. In deploy.rb add the following:

namespace :cache do
  desc “Clear memcach after deployment”
  task :clear, :roles => :app do
    run “cd #{current_release} && rake cache:clear RAILS_ENV=production”
  end
end

and (depending on your task chain):

after “deploy:update_code”, “cache:clear”

Hope it helps.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

odd number of arguments for Hash (ActionView::TemplateError)

Ruby on Rails 2 Comments »

This error can be caused by an outdated version of the globalize2 plugin, so try

 script/plugin remove globalize2

and

script/plugin install git://github.com/joshmh/globalize2.git

Hope it helps.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

RSpec and Webrat for Rails Integration Testing

RSpec, Ruby on Rails, Webrat No Comments »

Getting RSpec and Webrat to cooperate has gotten a bit easier recently, so make sure you have all the latest versions of the webrat, rspec and rspec-rails gems installed. Some quirks remain which might trip you up coming from TestUnit. For example if you

cd spec/integration

and

spec ./user_integration_spec.rb

or

ruby ./user_integration_spec.rb

you might get

undefined method `visit (…)

while a similar procedure would run fine in test/integration with TestUnit

But with RSpec, when you run

spec ./spec/integration/user_integration_spec.rb

or

rake spec:integration

all is well. It seems you have to stand at least in the spec directory or above, otherwise the block

Webrat.configure do |config|
  config.mode = :rails
  config.open_error_files = false # prevents webrat from opening the browser
end

in spec_helper.rb will not run correctly.

So remember that RSpec is a bit less forgiving than TestUnit in how your tests are called.

It’s also important that you run

script/generate rspec

after installing RSpec to have the rake tasks “installed”.

Finally, his procedure will likely not be necessary with recent versions of Webrat and RSpec.

See here for a testing system for Rails that integrates RSpec and Webrat, it makes a  lot of sense.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Using routing_filter with Devise

Ruby on Rails No Comments »

Having used Sven Fuchs’ routing_filter to localize routes (see here for alternative solutions), I now moved my authentication to devise from authlogic. This broke my localized routes with the default configuration, so for example

http://localhost:3000/en/users/sign_in

failed with an

ActionController::UnknownAction

error.

Fixing it was super-easy, simply open your config/initializers/devise.rb file and uncomment (currently line 81)

config.use_default_scope = true

and it should work like before.

Note that this works only with Rails 2.+, not with Rails 3 where it’s probably better to use a scope block to prepend locales to you routes, instead of the routing_filter plugin.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Using rails-footnotes with vim/gVim

Netbeans, Ruby on Rails No Comments »

Update March 7, 2011: The rails-footnotes gem/plugin as of version 3.6.7 finally works with Rails 3 and Rails > 2.3.6, meaning the project has been revived in a great way. One less reason for shirking the upgrade to Rails 3 and newer versions of rails 2.3.

Few gems or plugins speed up Rails development like rails-footnotes by José Valim and others. Especially useful is its ability to open files directly in the a text editor from Firefox.

Out of the box, this gem works with Textmate, but it’s quite easy to get it to work with Netbeans. To apply the same process for Vim/gVim integration, proceed like so:

Adjust Filter Prefix

Change your configuration in environment.rb of your Rails project to, for example:

if defined?(Footnotes)
Footnotes::Filter.prefix = ‘gvim://open?url=file://%s&line=%d&column=%d’
end

The protocol can be anything you like, this will now be configured in Firefox.

Configure Firefox

Open about:config in your Firefox location bar and

Right-Click > New > String “network.protocol-handler.app.gvim” with content ~/.editor_gvim.rb

Right-Click > New > Boolean “network.protocol-handler.external.gvim” with value true

Update: In Firefox 3.5 and above you have to do:

Right-Click: New > Boolean “network.protocol-handler.expose.gvim” with value false.

Then click on the link and select the ruby script that you created with the instructions below in the dialog. You can manage the assigned program in Edit > Preferences > Applications.

Create Editor Script

Create a script, for example in you home directory, and don’t forget to make it executable:

touch ~/editor_gvim.rb && chmod +x ~/editor_gvim.rb

The script could look like so:

#!/usr/local/bin/ruby
file = ARGV.first.split(’file://’).last.split(’&’).first
line = /\&line\=(\d+)/.match(ARGV.first)[1] rescue 0
`gvim –remote-tab-silent “+#{line}” #{file}`
`wmctrl -a “GVIM”`

The last two lines are delimited by backticks and it’s two hyphens (- -) before remote-tab-silent, which might all get eaten by Wordpress, so be sure to adjust after copying and pasting the code above.

Install wmctrl

sudo aptitude install wmctrl

Adjustments

The command in the script above

`gvim –remote-tab-silent “+#{line}” #{file}`

will open each file in a new tab.

If you want every file in a new buffer, simply change it to

`gvim –remote-silent “+#{line}” #{file}`

Finally, nothing stops you from opening the file in gVim and Netbeans simultaneously just by adding

`”/path/to/your/netbeans/executable” “#{file}:#{line}”`

`wmctrl -a “Netbeans IDE 6.5.1″`

NB will usually pick up all the changes automatically but in gVim you’ll have to reload with :e!

The Vim/gVim startup options are not easy to fathom (options go before files etc.), but here is the the documentation.

Update for RubyMine:

The relevant line to open RubyMine in your executable .rb file is the same as the new command line option to open a file on a specific line in RubyMine (from Version 3.2):

`mine –line #{line} #{file}`

This requires that you have created a Command-line launcher (under FIle > Create Command-line launcher).

Update:

Here’s a version which also sets the current vim directory (check with :pwd) to your rails root (prevents overwhelming fuzzy finder):

#!/usr/local/bin/ruby
file = ARGV.first.split(’file://’).last.split(’&’).first
line = /\&line\=(\d+)/.match(ARGV.first)[1] rescue 0
rails_root = /\&rails_root\=(.*)/.match(ARGV.first)[1] rescue ‘~’
`gvim –remote-silent “+cd #{rails_root}” “+#{line}” #{file}`
`”/home/dirk/netbeans-6.5.1/bin/netbeans” “#{file}:#{line}”`
`wmctrl -a “Netbeans IDE 6.5.1″`
`wmctrl -a “GVIM”`

With these settings in environment.rb:

if defined?(Footnotes)
Footnotes::Filter.prefix = “gvim://open?url=file://%s&line=%d&column=%d&rails_root=#{Rails.root}”
end

Vimperator

Now with the Vimperator plugin for Firefox you have round-trip development with gVim and Firefox without touching the mouse.

Hope it helps.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]
WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in