 |
Building a Ruby Sample for NetBeans
Posted by bleonard on March 26, 2007 at 03:26 PM | Comments (9)
We're in the process of creating some sample applications to ship with the NetBeans Ruby module, one of which will be based on the screencast, Putting Flickr on Rails. You can just download and run the sample project (you'll have to set your Flickr API Key), or build it from scratch as documented here.
Setting Things Up
- Download and install NetBeans 6.0 M7 or greater.
- Install the Ruby module from the update center (found in the Features folder. Many dependant modules will come along). The Ruby module requires a restart of the IDE.
- Apply for a Flickr API Key which you'll need to access the Flickr APIs.
- Get the Flickr library. Unless you've added gem to your path, browse to its location in your user directory and run gem install -r flickr.
Create the Search Dialog
In this step will create the Ruby on Rails application and a page for searching the Flickr database by tag.
- Create a new Ruby on Rails application named Flickr.
- The Flickr library expects you to add your Flickr API Key directly to its script. You could do that, however the approach here allows us to use the Flickr library without touching it. Open environment.rb and add the following to the bottom. Remember to enter your Flicker API Key.
require 'rubygems'
require 'flickr'
MY_KEY='Enter your Flicker API Key'
class Flickr
alias old_initialize initialize
def initialize(api_key=MY_KEY, email=nil, password=nil)
puts "new_initialize " + MY_KEY
old_initialize(api_key, email, password)
@host="http://api.flickr.com"
@activity_file='flickr_activity_cache.xml'
end
end
- Right-click the Views -> layouts folder and select New -> Empty RHTML (Embedded Ruby) file. Name the file application.
- Add the following HTML:
<html>
<head>
<title>Flickr</title>
<%= javascript_include_tag :defaults %>
<%= stylesheet_link_tag 'flickr' %>
</head>
<body>
<%= yield %>
</body>
</html>
- Right-click the Public -> stylesheets folder and select New -> File/Folder. Then select XML -> Cascading Style Sheet. Name if flickr. Add the following styles:
body {
background-color: #888;
font-family: Lucida Grande;
font-size: 11px;
margin: 25px;
}
form {
margin: 0;
margin-bottom: 10px;
background-color: rgb(222,231,236);
border: 5px solid #333;
padding: 25px;
}
fieldset {
border: none;
}
#spinner { float: right; margin: 10px; }
#photos img {
border: 1px solid #000;
width: 75px;
height: 75px;
margin: 5px;
}
- Right-click the Controllers node and select Generate. Set the name to flickr and the Views to index. Put the following in index.rhtml:
<%= form_remote_tag :url => {:action => 'search'}, :update => 'photos' %>
<fieldset>
<label for="tags">Tags:</label>
<%= text_field_tag 'tags' %>
<%= submit_tag 'Find' %>
</fieldset>
<div id="photos"></div>
<%= end_form_tag %
Pause to Test
Here we will configure the environment so that running the project launches our application:
- Stop the WEBrick server by clicking the red
in the Output window:
- Delete index.html from Public.
- Open routes.rb and add the following to the bottom of the file:
map.connect "", :controller => 'flickr'
- Press F6 to start WEBRick and launch the application.

Define the Search Method
- Switch to the FlickrController class and add the following:
class FlickrController < ApplicationController
def search
flickr = Flickr.new
render :partial => 'photo', :collection =>
flickr.photos(:tags => params[:tags], :per_page => '24')
end
end
- Right-click the Views -> flickr folder and select New -> Empty RHTML (Embedded Ruby) file. Name the file _photo and replace its contents with the following:
<img class='photo' src="<%= photo.sizes[0]['source'] %>">
- Return to your browser and enter a search string (give the images a couple of seconds to load):

Improve The User Experience
When you click find, there's no feedback that something's happening behind the scenes. We'll add a simple animated gif to help address this as well as change the effect of the images loading.
- Drag this animated gif
to your Public -> images folder.
- Modify index.rhtml as follows (the parts in bold were added)
<%= form_remote_tag :url => {:action => 'search'}, :update => 'photos',
:complete => visual_effect(:blind_down, 'photos'),
:before => %(Element.show('spinner')),
:success => %(Element.hide('spinner')) %>
<%= image_tag 'spinner.gif', :id => 'spinner', :style => 'display: none' %>
<fieldset>
<label for="tags">Tags:</label>
<%= text_field_tag 'tags' %>
<%= submit_tag 'Find' %>
</fieldset>
<div id="photos" style="display: none"></div>
<%= end_form_tag %>
- Return to your browser and try another search string.
Now you get a simple animation to let you know the server is working on your request and when the images do appear, they drop down like a set of blinds.
Bookmark blog post: del.icio.us Digg DZone Furl Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment
-
Wow, thank you so much for doing this Brian! By the way, have you noticed the Gem Manager in the Tools menu? You should be able to install flicker easily from within the GUI...
Posted by: tnorbye on March 26, 2007 at 04:20 PM
-
Brian, thanks for the great example code. I'm having a little trouble with NB M7 build and getting this working. After I've defined the Search method and try to search for a tag, I get the response,
" ActionView::ActionViewError in FlickrController#search
No rhtml, rxml, rjs or delegate template found for flickr/app/views"
Any ideas?
This is on NB6 M7 on OS X 10.4.9.
Posted by: moazamraja on March 27, 2007 at 02:17 AM
-
Brian, nevermind my previous question. Looks like I had the partial (_photo.rhtml) in the Views folder instead of the Views->flickr folder.
Now I'm just trying to figure out why the css stylesheet isn't working, but I'm sure it's something monitor.
Thanks again!
Posted by: moazamraja on March 27, 2007 at 02:39 AM
-
Brian,
nice work. I'd love to see a pure Ruby example too though, for those of us who aren't Railz0rz. Thanks.
Posted by: pate on March 27, 2007 at 05:59 AM
-
Shoot, I forget about the Gem Manager. However, trying it now gives me the following error:
:[-1,-1]:[0,0]: C:\Documents and Settings\Brian Leonard\.netbeans\dev\jruby-0.9.8\bin\gem.bat:0: syntax error, expecting end-of-file '\n' ',' ';' '=' '?' '[' kRESCUE kENSURE kEND kELSIF kELSE kWHEN kAND kOR kIF_MOD kUNLESS_MOD kWHILE_MOD kUNTIL_MOD kRESCUE_MOD tPOW tCMP tEQ tEQQ tNEQ tGEQ tLEQ tANDOP tOROP tMATCH tNMATCH tDOT tDOT2 tDOT3 tLSHFT tRSHFT tCOLON2 tOP_ASGN tRPAREN tSTAR2 tAMPER2 tPERCENT tDIVIDE tPLUS tMINUS tLT tGT tPIPE tCARET tRCURLY but found tIDENTIFIER instead (SyntaxError)
It looks like you want to be calling gem (the Ruby script) rather than gem.bat. If I rename gem to gem.bat, then the Gem Manager works as expected.
Posted by: bleonard on March 27, 2007 at 06:11 AM
-
I actually built this sample using pure Ruby. I run NetBeans with two userdirs, one that points to Ruby and another that points to JRuby. After creating the application, I simply opened it up in my JRuby version of NetBeans and it ran w/out change. The only thing I had to do was install the flickr gem, but that step is identical in both versions. So, the steps outlined and the project provided should work fine for you.
Posted by: bleonard on March 27, 2007 at 06:19 AM
-
Hi Brian, I didn't realize the Gem Manager was broken on Windows. Overnight two bugs were filed on it :) It's fixed now and integrated into version 0.48.
Posted by: tnorbye on March 27, 2007 at 11:09 AM
-
hi,this is Ravi vardhan
i have used your flickr application in NetBeans6.0 IDE
but.i got the error like,please reply me for this request
thanks
=====================================================
ArgumentError in FlickrController#search
wrong number of arguments (3 for 0)
RAILS_ROOT: ./script/../config/..
Application Trace | Framework Trace | Full Trace
#{RAILS_ROOT}/config/environment.rb:52:in `old_initialize'
#{RAILS_ROOT}/config/environment.rb:52:in `initialize'
#{RAILS_ROOT}/app/controllers/flickr_controller.rb:7:in `new'
#{RAILS_ROOT}/app/controllers/flickr_controller.rb:7:in `search'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:1095:in `send'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:1095:in `perform_action_without_filters'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:632:in `call_filter'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:619:in `perform_action_without_benchmark'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/benchmarking.rb:66:in `perform_action_without_rescue'
C:/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/benchmarking.rb:66:in `perform_action_without_rescue'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/rescue.rb:83:in `perform_action'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:430:in `send'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:430:in `process_without_filters'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:624:in `process_without_session_management_support'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/session_management.rb:114:in `process'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:330:in `process'
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/dispatcher.rb:41:in `dispatch'
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb:113:in `handle_dispatch'
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb:79:in `service'
C:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
C:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
C:/ruby/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
C:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start'
C:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
C:/ruby/lib/ruby/1.8/webrick/server.rb:95:in `start'
C:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `each'
C:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `start'
C:/ruby/lib/ruby/1.8/webrick/server.rb:23:in `start'
C:/ruby/lib/ruby/1.8/webrick/server.rb:82:in `start'
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb:63:in `dispatch'
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/servers/webrick.rb:59
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in `require'
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:342:in `new_constants_in'
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in `require'
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/server.rb:39
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
script/server:3
#{RAILS_ROOT}/config/environment.rb:52:in `old_initialize'
#{RAILS_ROOT}/config/environment.rb:52:in `initialize'
#{RAILS_ROOT}/app/controllers/flickr_controller.rb:7:in `new'
#{RAILS_ROOT}/app/controllers/flickr_controller.rb:7:in `search'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:1095:in `send'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:1095:in `perform_action_without_filters'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:632:in `call_filter'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:619:in `perform_action_without_benchmark'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/benchmarking.rb:66:in `perform_action_without_rescue'
C:/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/benchmarking.rb:66:in `perform_action_without_rescue'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/rescue.rb:83:in `perform_action'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:430:in `send'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:430:in `process_without_filters'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/filters.rb:624:in `process_without_session_management_support'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/session_management.rb:114:in `process'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:330:in `process'
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/dispatcher.rb:41:in `dispatch'
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb:113:in `handle_dispatch'
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb:79:in `service'
C:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
C:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
C:/ruby/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
C:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start'
C:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
C:/ruby/lib/ruby/1.8/webrick/server.rb:95:in `start'
C:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `each'
C:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `start'
C:/ruby/lib/ruby/1.8/webrick/server.rb:23:in `start'
C:/ruby/lib/ruby/1.8/webrick/server.rb:82:in `start'
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb:63:in `dispatch'
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/servers/webrick.rb:59
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in `require'
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:342:in `new_constants_in'
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in `require'
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/server.rb:39
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
script/server:3
Request
Parameters: {"commit"=>"Find", "tags"=>"india"}
Show session dump
---
flash: !map:ActionController::Flash::FlashHash {}
Response
Headers: {"cookie"=>[], "Cache-Control"=>"no-cache"}
Posted by: ravi_ruby on August 14, 2007 at 02:57 AM
-
Hi Ravi,
It appears that you have a typo in the search method of your FlickrController. Have you tried running the Flickr on Rails Sample App that's available in the NetBeans Plugin Manager (Tools > Plugins > New Plugins > More)?
Brian
Posted by: bleonard on August 14, 2007 at 05:48 AM
|