Installing Rails on Windows
We present two options for a Windows installation of Ruby on Rails: Instant Rails and RadRails alone and Instant Rails plus the Rad Rails IDE.
Instant Rails
The easiest way to get started on Windows is to use Instant Rails. Instant Rails (Figure A-1) is a one-stop Rails runtime solution containing Ruby, Rails, Apache, and MySQL, all preconfigured and ready to run. There is no installer, you simply unzip it into the directory of your choice and run it. It does not modify your system environment.
Figure A-1. Instant Rails
For more details about Instant Rails, go to the Instant Rails home page at http://instantrails.rubyforge.org:
-
Download and unzip the latest version of the Instant Rails ZIP file from: http://rubyforge.org/frs/?group_id=904.
-
Make sure that the installation path (to the directory into which you unzip the archive) does not contain any space characters, and then start InstantRails.exe.
-
Instant Rails will detect that it is being started from a new directory and ask whether you want to have it update the paths in the all of the configuration files. Click Yes.
-
Click on the “I” button (or press the Alt key twice) to display the main menu.
That’s all there is to it!
Instant Rails includes the cookbook Rails application from the ONLamp.com article “Rolling with Ruby on Rails” (http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html). The cookbook application is included as a preinstalled sample application. There is also a version of this tutorial that was rewritten to be specific to Instant Rails available at http://instantrails.rubyforge.org/wiki/wiki.pl?Rolling_With_Ruby_On_Instant_Rails_Tutorial. Instant Rails also includes the Photo Share application from this book.
Instant Rails includes the Apache web server, which you won’t use until later in the development of a Rails application, when you want to more closely duplicate your final deployment environment. During development, it is easiest use Ruby’s built-in web server, WEBrick, or the new Ruby Mongrel server.
So, for example, to run the cookbook application, execute the Instant Rails menu command Rails Applications ->>> Manage Rails Applications…, select the checkbox next to the cookbook application, and press the “Start with WEBrick” button. When you browse to http://127.0.0.1:3000/, you will see the cookbook application.
Instant Rails includes the One-Click Ruby Installer for Windows for its Ruby interpreter, which includes the SciTE text editor with full Ruby syntax highlighting. After installing Instant Rails, you can find the SciTE executable at InstantRails/ruby/scite/SciTE.exe.
RadRails
If you want more than a simple text editor, then try out the excellent RadRails IDE. RadRails (Figure A-2) is an Eclipse plug-in and is available as both a standalone IDE (Eclipse with the plug-in preinstalled) and as a standard Eclipse plug-in at http://www.radrails.org. With RadRails, you get a full IDE, complete with an integrated GUI debugger.
Figure A-2. RadRails
After you install RadRails, you have to configure it to work with your Instant Rails installation:
-
Execute the menu command Window ->>> Preferences.
-
Select Ruby ->>> Installed Interpreters.
-
Click the Add button, and give the new interpreter instance a name (like “Instant Rails Ruby”); browse to the Ruby executable at InstantRails/ruby/bin/ruby.exe, and click OK.
-
While still in the preferences dialog, select Ruby->Ri/rdoc and set the Rdoc and Ri paths to InstantRails/ruby/bin/rdoc and InstantRails/ruby/bin/ri, respectively. This step lets you use the built-in documentation features of RadRails.
You can create a new skeleton Rails application via the menus with File ->>> New… ->>> Rails ->>> RailsProject.
Filtering by Category
Displaying all unused photos might seem acceptable right now, but we have only nine photos. If there were 900, it would quickly become unusable. So, our final feature in this chapter will be to display only the unused photos in a particular category.
The first thing to do in our controller is get a list of all categories that can populate the drop-down selection box. Edit photos/app/controllers/slideshows_controller.rb, and add this line to the end of the edit method:
@all_categories = Category.find(:all, :order=>"name")
This line retrieves a list of categories that can populate a drop-down selection box that the user will use to display only those unused photos that are in the selected category.
Now, edit photos/app/views/slideshows/edit.rhtml, and add this right after the ‘Play this Slideshow’ line:
<p> <label for="category_id">Filter "Unused Photos"
on this Category</label><br/> <%= collection_select(:category, :id, @all_categories,
:id, :long_name) %>
<%= observe_field(:category_id,
:frequency => 2.0,
:update => 'slideshow-photos',
:url => { :action => 'change_filter'},
:with => 'category_id' ) %>
</p>
The collection_select helper is normally used inside an HTML form, but here we are using it because it conveniently knows how to display a collection in a drop-down box. It will never be submitted as part of a form.
As shown, the observe_field helper checks the category drop-down box for changes every two seconds. When a change is detected, an Ajax request is fired off to the change_filter method, which returns new HTML (that has been appropriately filtered) to replace the slideshow-photos section.
The Category model class automatically shows a collection of all photos that are in a particular category. However, we need to get a collection of photos that are in a given category and in all of its child categories.
Edit photos/app/models/category.rb, and add this method:
def photos_including_child_categories
result = photos.clone
children.each do |c|
c.photos_including_child_categories.each {|p|
result << p if not result.include? p}
end
result
end
This method recursively collects a list of all photos in its own category and all of its child categories. You can use this in to get the list of unused photos to display.
In the meantime, edit photos/app/controllers/slideshows_controller.rb to add the change_filter method:
def change_filter
slideshow_id = session[:slideshow].id
category_id = params[:category_id] || 1
session[:category_id] = category_id
@slideshow = Slideshow.find(slideshow_id)
session[:slideshow] = @slideshow
@photos = unused_photos(@slideshow)
render_partial ‘photo_picker’
end
This method stores the chosen category id in the session hash, retrieves a new list of unused photos, and then renders the photo_picker. Notice the bold code line in the previous code. This line tries to retrieve the category id from the request parameters. If there aren’t any parameters, params[:category_id] returns nil, and the || operator returns the rightmost argument (”1″ in this case).
Also, in this slideshow controller, we need to update the method that retrieves the unused photos to pay attention to the category setting. Do so by editing the unused_photos method; then replace the line all_photos = Photo.find(:all) with the following:
category_id = session[:category_id] || 1 session[:category_id] = category_id category = Category.find(category_id) all_photos = category.photos_including_child_categories
We’re done; we’ve added category filtering! Fire up your browser, and try it (you may need to assign some categories to some unused photos). Now it looks like Figure 1.
Figure 1. Filtering on categories
