Use rails-footnotes to open files in NetBeans from Firefox

Here is how to use rails-footnotes with NetBeans on linux!

Thanks to José Valim we have a nice new plugin for better debugging of Rails applications! In his blog post he describes how to open links to files on your hard disk with notepad++ on windows. I adapted this idea for opening files with netbeans on linux, and here is your way to go:

open about:config in firefox and add:

  • string network.protocol-handler.app.editor with content ~/.editor.rb
  • bool network.protocol-handler.external.editor and set it to true

create the file ~/.editor.rb and make it executable:

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

And here is the content:

#!/usr/bin/ruby
file = ARGV.first.split('file://').last.split('&').first
line = /\&line\=(\d+)/.match(ARGV.first)[1] rescue 0
`"/path/to/your/netbeans-dir/bin/netbeans" "#{file}:#{line}"`
`wmctrl -a "NetBeans IDE"`

install wmctrl

sudo aptitude install wmctrl

We’ll need this to give focus to the NetBeans window.

Tags: ,

14 Responses to “Use rails-footnotes to open files in NetBeans from Firefox”

  1. Bharat Says:

    Hello Johannes,
    I use netbeans on linux and I just installed Footnotes then I saw your post so proceeded to install it by following your directions above. I am not sure how to open “what” from firefox? When I run the application after installing footnotes, I see all the debugging links which I can click on to open and display the information, but how do I click on what links to display the code in NetBeans? Can you provide a bit more direction?
    Thanks.
    Bharat

  2. Bharat Says:

    OK. I got past not being able to see the links for controller etc. I had forgotten to write the following line in my development.rb file under config/environments directory:

    Footnotes::Filter.prefix = “editor://open?file://”

    Now I did everything as you said above. Here is my .editor.rb file in my home directory:

    bruparel@bcr-d810:~$ cat .editor.rb
    #!/usr/bin/ruby
    file = ARGV.first.split(’file://’).last.split(’&’).first
    line = /\&line\=(\d+)/.match(ARGV.first)[1] rescue 0
    `”/home/bruparel/netbeans-6.5/bin/netbeans” “#{file}:#{line}”`
    `wmctrl -a “NetBeans IDE”`
    bruparel@bcr-d810:~$ ll .editor.rb
    -rwxr-xr-x 1 bruparel bruparel 216 2009-03-30 20:41 .editor.rb

    And here are the entries from about:config in firefox:

    network.protocol-handler.app.editor;~./editor.rb
    network.protocol-handler.external.editor;true

    But when I click on the links that appear on my Firefox browser window in my application. here is the message that I get:

    Firefox doesn’t know how to open this address, because the protocol (editor) isn’t associated with any program.

    How do I fix that?

    Thanks for your time.

    Bharat

  3. Bharat Says:

    Sorry,
    I had mistyped the value for network.protocol-handler.app.editor;~./editor.rb
    It should have been network.protocol-handler.app.editor;~/.editor.rb

    I made that correction and restarted everything. When I click on the link now I get the following message from netbeans instead of firefox:

    /home/bruparel/editor:/open?file:/home/bruparel/exp/pizzaria-3/app/controllers/home_controller.rb&line=3&column=3 does not exist, or is not a plain file.

    I do not know how to fix it. Do you think that the following line in my Rails_root/config/environments/development.rb is wrong?

    Footnotes::Filter.prefix = “editor://open?file://”

    This is wonderful. If I can get this to work, I will buy a you a whole pack of beer when you are in Boston.

  4. Johannes Leers Says:

    hey Bharat,
    it is nice you figured out the most of the errors yourself.
    nevertheless i will try to earn a pack of boston beer :)

    in my opinion the error is indeed the line in your environment.rb. try to change it to
    Footnotes::Filter.prefix = ‘editor://open?url=file://%s&line=%d&column=%d’

    it would be nice when you report your progress!
    Johannes

  5. Bharat Says:

    Hello Yohannes,
    Thanks for getting back to me. I replaced the line of code in my development.rb with the one that you have above:
    Footnotes::Filter.prefix = ‘editor://open?url=file://%s&line=%d&column=%d’

    First, NetBeans started complaining about unexpected ? mark in the editor. I think it is having trouble parsing the enclosing ‘ character that you have around the string ‘editor://open?url=file://%s&line=%d&column=%d’. I replaced it with the single quote character at both ends (’) and it parsed it correctly. but when I ran the program and clicked on the link, i got the same message. Next, I tried double quotes instead of single quotes and got the same result.

    Next, I replaced both single quote character with back-tick characters as shown below:

    Footnotes::Filter.prefix = `editor://open?url=file://%s&line=%d&column=%d`

    And restarted the program again to reload it. This time I got the following message from the browser:

    Routing Error

    No route matches “/home/bruparel/exp/pizzeria-3/app/controllers/home_controller.rb&line=3&column=3″ with {:method=>:get}

    The URL that it shows in the browser is as follows:

    http://localhost:3000/home/bruparel/exp/pizzeria-3/app/controllers/home_controller.rb&line=3&column=3

    I am on Ubuntu 8.10 and Firefox 3.0.8.

    I am afraid you will have to work a bit harder for the Sam Adams beer my friend.

    Look forward to hear from you.

    Bharat

  6. Johannes Leers Says:

    ok, wordpress (the blogging engine i use) converts that single quotes to grave and accute accents but single quotes are definitely correct (as are double quotes). btw backticks (grave accutes) are the same as calling system, see here.

    you write you get ‘the same message’ after replacing with single quotes. do you mean the message from netbeans you stated here?
    /home/bruparel/editor:/open?file:/home/bruparel/exp/pizzaria-3/app/controllers/home_controller.rb&line=3&column=3 does not exist, or is not a plain file.

    when you hover the footnotes, can you post the link from Controller? it should be something like:
    editor://open?url=file:///home/username/folder-to-your-application/app/controllers/example_controller.rb&line=5&column=3

    Johannes

  7. Bharat Says:

    Ok Yohannes. here is the result of hovering over the footnotes link:

    editor://open?url=file://%s&line=%d&column=%d/home/bruparel/exp/pizzeria-3/app/controllers/home_controller.rb&line=3&column=3

    I see that it is quite a bit different than yours and may be that is the problem. Though I am not sure why the difference. Here is my ~/.editor.rb contents:

    bruparel@bcr-d810:~$ cat ~/.editor.rb
    #!/usr/bin/ruby
    file = ARGV.first.split(’file://’).last.split(’&’).first
    line = /\&line\=(\d+)/.match(ARGV.first)[1] rescue 0
    `”/home/bruparel/netbeans-6.5/bin/netbeans” “#{file}:#{line}”`
    `wmctrl -a “NetBeans IDE”`
    bruparel@bcr-d810:~$

    Thanks for your patience and time.

    Bharat

  8. Johannes Leers Says:

    hmm…
    which rails version do you use? you’ll find this information in the first lines of your config/enviroment.rb
    which rails-footnotes version do you use? you’ll find this information in vendor/plugins/rails-footnotes/README or vendor/plugins/rails-footnotes/CHANGELOG

    Note, this howto is for the ‘new’ rails-footnotes made by josevalim so you’ll need at least a rails version >= 2.1 and rails-footnotes version >= 3.2.2
    as i see it you are using at least a lower version of rails-footnotes?!

  9. Bharat Says:

    This is from my config/environment file:

    # Specifies gem version of Rails to use when vendor/rails is not present
    RAILS_GEM_VERSION = ‘2.1.2′ unless defined? RAILS_GEM_VERSION

    And this is from vendor/plugins/rails-footnotes/CHANGELOG

    TODO:
    * Create ProfilerNote

    == Footnotes v3.2.2
    Author: José Valim (jose.valim@gmail.com)
    Site: http://www.pagestacker.com/
    * Added trace to QueriesNote;
    * Fixed incompatibility with Ultrasphinx (thanks to mhartl);
    * Added W3C compatibility (thanks to tapajos);
    * Added support to other log mechanisms in LogNote.

    I was careful in installing the correct version. By the way, I am on NetBeans 6.5.

  10. Johannes Leers Says:

    yep, i expected this. so you cannot use the script of my blog post because as i see it, this version of rails-footnotes doesn’t support the ‘line’ command. try this adapted script instead of the ~/.editor.rb above:

    #!/usr/bin/ruby
    file = ARGV.first.split('file://').last.split('&').first
    `"/path/to/your/netbeans-dir/bin/netbeans" "#{file}"`
    `wmctrl -a "NetBeans IDE"`

    remember to change the line in the config/environment.rb back to:
    Footnotes::Filter.prefix = "editor://open?file://" as stated here.
    note it is not working for me writing this in the config/environments/development.rb but it works when in config/environment.rb.

    when you get errors about “Footnotes not defined” try to do:

    if defined?(Footnotes)
    Footnotes::Filter.prefix = "editor://open?file://"
    end

  11. Bharat Says:

    No luck Yohannes. I have requested the NetBeans lead for Ruby to release a patch for Rails 2.3.2 for NetBeans 6.5 so that I can use your original recipe. Generally, they are very responsive to our needs so I expect Erno to do something fairly soon. I will keep you posted on how it works out.
    Thanks a lot for trying. I still owe you that case of beer, you just have to come to Boston :) . Seriously, if you are in here at all, look me up. We have pretty good Ruby and Rails community here.
    Regards,
    Bharat

  12. Johannes Leers Says:

    ok so good luck. once you have rails-footnotes working it is really a timesaver!
    and believe me, once i am at boston i’ll come and visit you to drink some beers :)

  13. Dirk Says:

    Thanks Johannes, I got it working after changing the shebang in .editor.rb to
    #!/usr/local/bin/ruby
    which is the ruby executable path when building Ruby 1.87 from source on Ubuntu and probably other configurations.
    After installing the Netbeans fix suggested by Bharat which is at
    http://www.netbeans.org/issues/show_bug.cgi?id=162287
    it works great now save for some minor quirks. Thanks again!

  14. Dirk Says:

    And when upgrading to Firefox 3 it might be necessary to remove (right-click and reset) the above settings in about:config, restart Firefox and then recreate the settings. Then, upon clicking a footnotes link, FF 3 will run a permissions dialog and put .editor.rb into the list at Edit > Preferences > Applications which is probably necessary for FF 3 to run external apps.

Leave a Reply