Dmitry Galinsky just released a rails minor mode (page on rubyforge ) that ties many features into one concise location:
Features (svn version):
ECB allows project/file management and supports fixed windows for integrated buffers for shells/logs/consoles/etc that don’t interfere with file/window management.
Here’s a list of the snippets taken from the rails.el, to use them, type the code and hit the tab key:
| ra | render-action |
| ral | render-action-layout |
| rf | render-file |
| rfu | render-file-use_full_path |
| ri | render-inline |
| ril | render-inline-locals |
| rit | render-inline-type |
| rl | render-layout |
| rn | render-nothing |
| rns | render-nothing-status |
| rp | render-partial |
| rpc | render-partial-collection |
| rpl | render-partial-locals |
| rpo | render-partial-object |
| rps | render-partial-status |
| rt | render-text |
| rtl | render-text-layout |
| rtlt | render-text-layout_true |
| rts | render-text-status |
| rcea | render-component-action |
| rcec | render-component-controller |
| rceca | render-component-controller-action |
| rea | redirect_to-action |
| reai | redirect_to-action-id |
| rec | redirect_to-controller |
| reca | redirect_to-controller-action |
| recai | redirect_to-controller-action-id |
| flash | flash |
| logi | logger.info |
| par | params |
| ses | session |
| belongs | belongs_to-class-foreign_key |
| many | has_many-class-foreign_key-dependent |
| one | has_one-class-foreign_key-dependent |
| valpres | validates_presence_of |
| valuniq | validates_uniqueness_of |
| valnum | validates_numericality_of |
| ft | form_tag-action |
| lia | link_to-action |
| liai | link_to-action-id |
| lic | link_to-controller |
| lica | link_to-controller-action |
| licai | link_to-controller-action-id |
| %h | h … |
| %if | if … end |
| %unless | unless … end |
| #ifel | if … else … end |
| % | <% … %> |
| %% | <%= … %> |
If you’re starting out with a clean build of GNU/Emacs, you might want to install:
Note: I kept having problems with things not working as advertised with different minor-modes, so I gave in and installed the cvs head version of emacs 22.0.50.1. WOW. Everything started working correctly. MMM auto-indented, HTML mode indent/code-complete works, and on and on. I would recommend this as your first step, if you haven’t upgraded already. There’s a good quick tutorial for MacOS (which got me where I needed with my slackware box) right here .
Your .emacs might look vaguely like the following (I’ll update it soon). I have my .el files in /opt/emacs and use GNU Emacs:
(setq x-select-enable-clipboard t)
(setq interprogram-paste-function 'x-cut-buffer-or-selection-value)
(add-to-list 'load-path
"/opt/emacs")
(require 'cedet)
;; Enabling various SEMANTIC minor modes. See semantic/INSTALL for more ideas.
;; Select one of the following
(semantic-load-enable-code-helpers)
;; (semantic-load-enable-guady-code-helpers)
;; (semantic-load-enable-excessive-code-helpers)
;; Enable this if you develop in semantic, or develop grammars
;; (semantic-load-enable-semantic-debugging-helpers)
(require 'ecb-autoloads)
(autoload 'ruby-mode "ruby-mode" "Ruby editing mode." t)
(setq auto-mode-alist (cons '("\\.rb$" . ruby-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("\\.rhtml$" . html-mode) auto-mode-alist))
(modify-coding-system-alist 'file "\\.rb$" 'utf-8)
(modify-coding-system-alist 'file "\\.rhtml$" 'utf-8)
(require 'snippet)
(require 'rails)
(defun try-complete-abbrev (old)
(if (expand-abbrev) t nil))
(setq hippie-expand-try-functions-list
'(try-complete-abbrev
try-complete-file-name
try-expand-dabbrev))
(add-hook 'ruby-mode-hook
(lambda()
(add-hook 'local-write-file-hooks
'(lambda()
(save-excursion
(untabify (point-min) (point-max))
(delete-trailing-whitespace)
)))
(set (make-local-variable 'indent-tabs-mode) 'nil)
(set (make-local-variable 'tab-width) 2)
(imenu-add-to-menubar "IMENU")
(require 'ruby-electric)
(ruby-electric-mode t)
))
See emacs-rails, ECB and Ruby Electric modes in action
With this function and script you can automatically open rails files with errors.
Add this function to your .emacs file:
(defun rails-find-and-goto-error () "Finds error in rails html log go on error line" (interactive) (search-forward-regexp "RAILS_ROOT: \\([^<]*\\)") (let ((rails-root (concat (match-string 1) "/"))) (search-forward "id=\"Application-Trace\"") (search-forward "RAILS_ROOT}") (search-forward-regexp "\\([^:]*\\):\\([0-9]+\\)") (let ((file (match-string 1)) (line (match-string 2))) ;(kill-buffer (current-buffer)) (message (format "Error found in file \"%s\" on line %s. " file line)) (find-file (concat rails-root file)) (goto-line (string-to-int line))))) </pre>Now create script rails-auto-error.
Unix version:
#!/bin/sh #FvwmCommand "All (emacs) WarpToWindow 50 50"gnudoit.emacs “(find-file \”$1\“) (rails-find-and-goto-error)”
Now configure your browser to open html code with rails-auto-error and bind it on some key (I use firefox exstansion ViewSourceWith (http://dafizilla.sourceforge.net/viewsourcewith/))
Rails error on unix
Simple script to open emacs on line with error (xclip and gnuserv needed):
#!/bin/sh qu=`xclip -o` file=`echo $qu|cut -d":" -f 1` line=`echo $qu|cut -d":" -f 2` #gnudoit.emacs "(rails-open-file \"$file\") (goto-line $line)" gnudoit.emacs "(find-file (concat (rails-root) \"$file\")) (goto-line $line)"Select string with error in browser and run this script.
.rhtml files
I’ve had some success using two-mode-mode to have both Ruby and HTML modes when editing .rhtml files. I still need to figure out how to let emacs’ psgml mode know that the .rhtml file belongs ‘inside’ another file so that it picks up the DOCTYPE.
To tell the truth, I wrote two-mode-mode, so suggestions and patches are welcome:-) – DavidWelton
Troubleshooting:
When ctr-t toggling inside a controller, I get the following error: ’Symbol’s function definition is void: posn-at-point’.
This is because the posn-at-point is only in the CVS (22) version of emacs that’s not yet stable. Here are two solutions from Dmitry:
- Probably it is necessary to update emacs to CVS HEAD. Or if updating is not desirable…
- Try modifying your rails.el:
line 595: remove >> (let ((pos (nth 2 (posn-at-point)))); add >> (let ((pos (cons 200 100)))When I type ‘rp TAB’, I get “Symbol’s function definition is void: rails-snip-rp”!
I got this to work by adding a load("rails") line after (require ‘rails), to run the snippet definitions correctly. There’s probably a better way of doing this!
—
http://rubyforge.org/tracker/index.php?func=detail&aid=3884&group_id=1484&atid=5809remove your ~/.abbrev_defs and restart emacs
—
When I type ‘ra TAB’, I get a Lisp overlay error (from overlay.el): “cannot support overlay ’modification-hooks property under XEmacs”. Ideas anyone?
—
Anyone know how to get this to work with nxml-mode instead of html-mode? I tried changing:
(unless (boundp ’nxml-mode-abbrev-table)
and
(def-snips (nxml-mode-abbrev-table nxml-helper-mode-abbrev-table)
in rails-ui.el, but it still won’t give me snippets in nxml-mode..
—
Try to switch on abbrev-mode in nxml-mode (M-x abbrev-mode), if snippet start working add hooksomthing like:
(add-hook ‘nxml-mode-hook (lambda ()
(abbrev-mode t))
—
doesn’t work. Let me know if you’re able to figure out how to get snippets to work with nxml-mode, ‘cause I’d really like to use emacs-rails, but I really need nxml-mode (it’s much nicer than html-mode). Thanks
—
I had the same problem, so I added this to my .emacs:
(add-hook ’nxml-mode-hook
(lambda ()
(setq local-abbrev-table nxml-mode-abbrev-table)))
arorem.el
[http://dev.technomancy.us/phil/wiki/arorem Arorem] is Another Ruby on Rails Emacs Mode.
Arorem is opinionated against mmm-mode, which seems to be more trouble than it’s worth. It’s difficult to set up, and having to reparse the buffer all the time gets old fast. Rather than try to mix major modes on the fly, it comes with an rhtml mode that is derived from html-mode and includes ruby syntax rules.
snippet.el and find-recursive.el
Looks like some Emacs installations have no these two files required for rails minor mode to run.
snippet.el which works with current SVN version (rev. 44) is here: http://www.kazmier.com/computer/snippet.el
find-recursive.el is here:
http://www.webweavertech.com/ovidiu/emacs/find-recursive.txtArgs out of range errors
Should you find yourself unable to get ruby mode or rails mode to load due to ‘Args out of range’ errors related to $Revision$, edit the ruby-mode.el file to have a valid revision number, rather than the string $Revision$, in the defconst of ruby-mode-revision. It’s a side-effect of not using subversion to get the ruby-mode.el file.
Emacs and ri
When you install gems such as Rails, there is no ri documentation generated for it by default. You can do this manually by changing to your gems directory and running “rdoc -Y�?, but it takes a very long time to do. Once the ri is generated, use [http://rubyforge.org/projects/ri-emacs Ri for Emacs] to access it.
See Also
http://lathi.net/twiki-bin/view/Main/EmacsAndRuby
Emacs on MacOS X
(Instructions are relevant to 10.4, may or may not be relevant to others. Read on).If you run Emacs on Mac OS X, you may find a number of failings whenever you attempt to run Ruby code from Mac OS X. For example, webrick fails to start up.
If you are like me, you rebuilt Ruby and placed it in /usr/local/bin, and then modified your PATH environment variable to search /usr/local/bin before Apple’s broken Ruby install at /usr/bin.
Unfortunately, Carbon Emacs (or any other Emacs instantiation that runs from an app bundle) does not read the environment variables set when you run Terminal. One solution is to rename /usr/bin/ruby out of the way, and create a symlink from /usr/bin/ruby to /usr/local/bin/ruby.
If you want to test if this is afflicting you, do this:
In config/boot.rb, just before require ‘rubygems’, add puts( $: ). Then, in Emacs, type rails-webrick:start-default-env, and switch to the WEBrick buffer. The list of directories that shows up before the “=> Booting Webrick…” line should correspond to where rubygems are installed.
—
re: carbon emacs and environment –
you can always do something like:
(add-to-list ’exec-path “/opt/local/bin”)
(add-to-list ’exec-path “/usr/local/mysql/bin”)
(add-to-list ’exec-path “/opt/local/sbin”)
(add-to-list ’exec-path (getenv "PATH"))
insteadAnother solution is to create a file called
~/.MacOSX/environment.plist
In that file you can specify any environment variables that you want Carbon Emacs to read when it starts up, including PATH. The file should look like this:<?xml version="1.0" encoding="UTF-8"?> <plist version="0.9"> <dict> <key>HOME</key> <string>/Users/you</string> <key>PATH</key> <string>/usr/local/bin:/sbin:/bin:/usr/bin</string> <key>JAVA_HOME</key> <string>/usr</string> <key>FOO</key> <string>BAR</string> </dict> </plist>
Dmitry Galinsky just released a rails minor mode (page on rubyforge ) that ties many features into one concise location:
Features (svn version):
ECB allows project/file management and supports fixed windows for integrated buffers for shells/logs/consoles/etc that don’t interfere with file/window management.
Here’s a list of the snippets taken from the rails.el, to use them, type the code and hit the tab key:
| ra | render-action |
| ral | render-action-layout |
| rf | render-file |
| rfu | render-file-use_full_path |
| ri | render-inline |
| ril | render-inline-locals |
| rit | render-inline-type |
| rl | render-layout |
| rn | render-nothing |
| rns | render-nothing-status |
| rp | render-partial |
| rpc | render-partial-collection |
| rpl | render-partial-locals |
| rpo | render-partial-object |
| rps | render-partial-status |
| rt | render-text |
| rtl | render-text-layout |
| rtlt | render-text-layout_true |
| rts | render-text-status |
| rcea | render-component-action |
| rcec | render-component-controller |
| rceca | render-component-controller-action |
| rea | redirect_to-action |
| reai | redirect_to-action-id |
| rec | redirect_to-controller |
| reca | redirect_to-controller-action |
| recai | redirect_to-controller-action-id |
| flash | flash |
| logi | logger.info |
| par | params |
| ses | session |
| belongs | belongs_to-class-foreign_key |
| many | has_many-class-foreign_key-dependent |
| one | has_one-class-foreign_key-dependent |
| valpres | validates_presence_of |
| valuniq | validates_uniqueness_of |
| valnum | validates_numericality_of |
| ft | form_tag-action |
| lia | link_to-action |
| liai | link_to-action-id |
| lic | link_to-controller |
| lica | link_to-controller-action |
| licai | link_to-controller-action-id |
| %h | h … |
| %if | if … end |
| %unless | unless … end |
| #ifel | if … else … end |
| % | <% … %> |
| %% | <%= … %> |
If you’re starting out with a clean build of GNU/Emacs, you might want to install:
Note: I kept having problems with things not working as advertised with different minor-modes, so I gave in and installed the cvs head version of emacs 22.0.50.1. WOW. Everything started working correctly. MMM auto-indented, HTML mode indent/code-complete works, and on and on. I would recommend this as your first step, if you haven’t upgraded already. There’s a good quick tutorial for MacOS (which got me where I needed with my slackware box) right here .
Your .emacs might look vaguely like the following (I’ll update it soon). I have my .el files in /opt/emacs and use GNU Emacs:
(setq x-select-enable-clipboard t)
(setq interprogram-paste-function 'x-cut-buffer-or-selection-value)
(add-to-list 'load-path
"/opt/emacs")
(require 'cedet)
;; Enabling various SEMANTIC minor modes. See semantic/INSTALL for more ideas.
;; Select one of the following
(semantic-load-enable-code-helpers)
;; (semantic-load-enable-guady-code-helpers)
;; (semantic-load-enable-excessive-code-helpers)
;; Enable this if you develop in semantic, or develop grammars
;; (semantic-load-enable-semantic-debugging-helpers)
(require 'ecb-autoloads)
(autoload 'ruby-mode "ruby-mode" "Ruby editing mode." t)
(setq auto-mode-alist (cons '("\\.rb$" . ruby-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("\\.rhtml$" . html-mode) auto-mode-alist))
(modify-coding-system-alist 'file "\\.rb$" 'utf-8)
(modify-coding-system-alist 'file "\\.rhtml$" 'utf-8)
(require 'snippet)
(require 'rails)
(defun try-complete-abbrev (old)
(if (expand-abbrev) t nil))
(setq hippie-expand-try-functions-list
'(try-complete-abbrev
try-complete-file-name
try-expand-dabbrev))
(add-hook 'ruby-mode-hook
(lambda()
(add-hook 'local-write-file-hooks
'(lambda()
(save-excursion
(untabify (point-min) (point-max))
(delete-trailing-whitespace)
)))
(set (make-local-variable 'indent-tabs-mode) 'nil)
(set (make-local-variable 'tab-width) 2)
(imenu-add-to-menubar "IMENU")
(require 'ruby-electric)
(ruby-electric-mode t)
))
See emacs-rails, ECB and Ruby Electric modes in action
With this function and script you can automatically open rails files with errors.
Add this function to your .emacs file:
(defun rails-find-and-goto-error () "Finds error in rails html log go on error line" (interactive) (search-forward-regexp "RAILS_ROOT: \\([^<]*\\)") (let ((rails-root (concat (match-string 1) "/"))) (search-forward "id=\"Application-Trace\"") (search-forward "RAILS_ROOT}") (search-forward-regexp "\\([^:]*\\):\\([0-9]+\\)") (let ((file (match-string 1)) (line (match-string 2))) ;(kill-buffer (current-buffer)) (message (format "Error found in file \"%s\" on line %s. " file line)) (find-file (concat rails-root file)) (goto-line (string-to-int line))))) </pre>Now create script rails-auto-error.
Unix version:
#!/bin/sh #FvwmCommand "All (emacs) WarpToWindow 50 50"gnudoit.emacs “(find-file \”$1\“) (rails-find-and-goto-error)”
Now configure your browser to open html code with rails-auto-error and bind it on some key (I use firefox exstansion ViewSourceWith (http://dafizilla.sourceforge.net/viewsourcewith/))
Rails error on unix
Simple script to open emacs on line with error (xclip and gnuserv needed):
#!/bin/sh qu=`xclip -o` file=`echo $qu|cut -d":" -f 1` line=`echo $qu|cut -d":" -f 2` #gnudoit.emacs "(rails-open-file \"$file\") (goto-line $line)" gnudoit.emacs "(find-file (concat (rails-root) \"$file\")) (goto-line $line)"Select string with error in browser and run this script.
.rhtml files
I’ve had some success using two-mode-mode to have both Ruby and HTML modes when editing .rhtml files. I still need to figure out how to let emacs’ psgml mode know that the .rhtml file belongs ‘inside’ another file so that it picks up the DOCTYPE.
To tell the truth, I wrote two-mode-mode, so suggestions and patches are welcome:-) – DavidWelton
Troubleshooting:
When ctr-t toggling inside a controller, I get the following error: ’Symbol’s function definition is void: posn-at-point’.
This is because the posn-at-point is only in the CVS (22) version of emacs that’s not yet stable. Here are two solutions from Dmitry:
- Probably it is necessary to update emacs to CVS HEAD. Or if updating is not desirable…
- Try modifying your rails.el:
line 595: remove >> (let ((pos (nth 2 (posn-at-point)))); add >> (let ((pos (cons 200 100)))When I type ‘rp TAB’, I get “Symbol’s function definition is void: rails-snip-rp”!
I got this to work by adding a load("rails") line after (require ‘rails), to run the snippet definitions correctly. There’s probably a better way of doing this!
—
http://rubyforge.org/tracker/index.php?func=detail&aid=3884&group_id=1484&atid=5809remove your ~/.abbrev_defs and restart emacs
—
When I type ‘ra TAB’, I get a Lisp overlay error (from overlay.el): “cannot support overlay ’modification-hooks property under XEmacs”. Ideas anyone?
—
Anyone know how to get this to work with nxml-mode instead of html-mode? I tried changing:
(unless (boundp ’nxml-mode-abbrev-table)
and
(def-snips (nxml-mode-abbrev-table nxml-helper-mode-abbrev-table)
in rails-ui.el, but it still won’t give me snippets in nxml-mode..
—
Try to switch on abbrev-mode in nxml-mode (M-x abbrev-mode), if snippet start working add hooksomthing like:
(add-hook ‘nxml-mode-hook (lambda ()
(abbrev-mode t))
—
doesn’t work. Let me know if you’re able to figure out how to get snippets to work with nxml-mode, ‘cause I’d really like to use emacs-rails, but I really need nxml-mode (it’s much nicer than html-mode). Thanks
—
I had the same problem, so I added this to my .emacs:
(add-hook ’nxml-mode-hook
(lambda ()
(setq local-abbrev-table nxml-mode-abbrev-table)))
arorem.el
[http://dev.technomancy.us/phil/wiki/arorem Arorem] is Another Ruby on Rails Emacs Mode.
Arorem is opinionated against mmm-mode, which seems to be more trouble than it’s worth. It’s difficult to set up, and having to reparse the buffer all the time gets old fast. Rather than try to mix major modes on the fly, it comes with an rhtml mode that is derived from html-mode and includes ruby syntax rules.
snippet.el and find-recursive.el
Looks like some Emacs installations have no these two files required for rails minor mode to run.
snippet.el which works with current SVN version (rev. 44) is here: http://www.kazmier.com/computer/snippet.el
find-recursive.el is here:
http://www.webweavertech.com/ovidiu/emacs/find-recursive.txtArgs out of range errors
Should you find yourself unable to get ruby mode or rails mode to load due to ‘Args out of range’ errors related to $Revision$, edit the ruby-mode.el file to have a valid revision number, rather than the string $Revision$, in the defconst of ruby-mode-revision. It’s a side-effect of not using subversion to get the ruby-mode.el file.
Emacs and ri
When you install gems such as Rails, there is no ri documentation generated for it by default. You can do this manually by changing to your gems directory and running “rdoc -Y�?, but it takes a very long time to do. Once the ri is generated, use [http://rubyforge.org/projects/ri-emacs Ri for Emacs] to access it.
See Also
http://lathi.net/twiki-bin/view/Main/EmacsAndRuby
Emacs on MacOS X
(Instructions are relevant to 10.4, may or may not be relevant to others. Read on).If you run Emacs on Mac OS X, you may find a number of failings whenever you attempt to run Ruby code from Mac OS X. For example, webrick fails to start up.
If you are like me, you rebuilt Ruby and placed it in /usr/local/bin, and then modified your PATH environment variable to search /usr/local/bin before Apple’s broken Ruby install at /usr/bin.
Unfortunately, Carbon Emacs (or any other Emacs instantiation that runs from an app bundle) does not read the environment variables set when you run Terminal. One solution is to rename /usr/bin/ruby out of the way, and create a symlink from /usr/bin/ruby to /usr/local/bin/ruby.
If you want to test if this is afflicting you, do this:
In config/boot.rb, just before require ‘rubygems’, add puts( $: ). Then, in Emacs, type rails-webrick:start-default-env, and switch to the WEBrick buffer. The list of directories that shows up before the “=> Booting Webrick…” line should correspond to where rubygems are installed.
—
re: carbon emacs and environment –
you can always do something like:
(add-to-list ’exec-path “/opt/local/bin”)
(add-to-list ’exec-path “/usr/local/mysql/bin”)
(add-to-list ’exec-path “/opt/local/sbin”)
(add-to-list ’exec-path (getenv "PATH"))
insteadAnother solution is to create a file called
~/.MacOSX/environment.plist
In that file you can specify any environment variables that you want Carbon Emacs to read when it starts up, including PATH. The file should look like this:<?xml version="1.0" encoding="UTF-8"?> <plist version="0.9"> <dict> <key>HOME</key> <string>/Users/you</string> <key>PATH</key> <string>/usr/local/bin:/sbin:/bin:/usr/bin</string> <key>JAVA_HOME</key> <string>/usr</string> <key>FOO</key> <string>BAR</string> </dict> </plist>