Search code examples
ruby-on-railsrubyvimautocompleteomnicomplete

Vim ruby, rails omnicompletion: "Error loading rails environment". How do I configure vim-ruby's autocomplete to work with rails?


I'm trying to get vim-ruby's autocompletion working with vim-rails. So far, I'm able to do local keyword completion with existing methods and variables in the current file, using <C-x><C-n> (although prefixes for instance variables and symbols are not preserved). I'd like to be able to autocomplete Rails methods like redirect_to and has_many. However, when I try to use omni-completion to accomplish this task, e.g. redir<C-x><C-o>, Vim tells me:

Error loading rails environment
Press ENTER or type command to continue

When I press ENTER, Vim tells me:

-- Omni completion (^O^N^P) Pattern not found

vim-ruby's autocompletion works for plain Ruby files, so that Arr<C-x><C-o> completes to Array, and in turn Array.<C-x><C-o> offers a list of methods on Array to choose from. When I try this in a Rails file, I get the same error as above; however, when I press ENTER to dismiss the error, ruby's autocompletion works as expected.

My question is, how do I properly configure vim-ruby so it can load the Rails environment? I assume that it's a vim-ruby problem and not vim-rails because commands like :Econtroller <controllername> work as expected.

My .vimrc contains the following:

set nocompatible
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'gmarik/Vundle.vim'
Plugin 'flazz/vim-colorschemes'
Plugin 'slim-template/vim-slim'
Plugin 'vim-ruby/vim-ruby'
Plugin 'tpope/vim-rails'
Plugin 'tsaleh/vim-shoulda'
Plugin 'ervandew/supertab'
call vundle#end()

autocmd FileType ruby set omnifunc=rubycomplete#Complete
let g:rubycomplete_buffer_loading = 1
let g:rubycomplete_classes_in_global = 1
let g:rubycomplete_rails = 1

Solution

  • Upon further investigation, vim-ruby was executing the system's ruby, with which I wasn't able to get rubygems to work. I'm not absolutely sure, but I believe vim-ruby/autoload/rubycomplete.vim was failing at this line while attempting to require the console_app gem, which didn't exist on my system.

    So I re-built vim according to the instructions on Jon Cairns' blog. The article references RVM, but is equally applicable to rbenv or whatever ruby you might be using.

    For posterity's sake, on Ubuntu, the steps I took to do this were:

    sudo apt-get install mercurial

    hg clone https://vim.googlecode.com/hg/ vim && cd vim

    ./configure --with-features=HUGE --enable-pythoninterp=yes --enable-multibyte=yes --enable-rubyinterp=yes --with-ruby-command=$(which ruby) --enable-gui=gnome2 --with-x --enable-fontset

    make

    sudo make install

    After doing this, omni-completion in Rails works like a charm - albeit a bit slow the first time it's used.