RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Build a Wiki System with Rails : Page 4

Learn how to build a simple, file-based Wiki system using the Ruby on Rails web application framework. You won't believe how easy it is.

Integrating the Model, View, and Controller Code
Now that you have coded and tested the Wiki model class, you can begin integrating your model, view, and controller code. Listing 2 shows the complete code for your WikiController class, found under app/controllers/wiki_controller.rb.

Recall that when you ran the Rails script/generate controller command earlier, it generated a few stub files. However, I added code to the generated methods and also added some additional methods that are not tied to any view but serve as specific actions used by other views. The list of public methods in WikiController includes: initialize, index, edit, view, print, help, create, delete, and save. Let's take a closer look at some of these methods:

  • The initialize method sets the name of the directory where your Wiki files will be stored. This name is configured in the config/environment.rb file via the WIKI_DIR property—this is the only configuration you will need to change to store your Wiki files under a more appropriate directory, if needed:
    WIKI_DIR = "/tmp/railswiki"
  • The index method calls Wiki.find_wikis to generate a list of existing Wiki file names and passes it onto the index.rhtml view using the instance variable @filelist. This view loops through the array and populates a HTML <SELECT> drop down box:
    <select name="f">
        <% @filelist.each do |file| %>
            <option><%= file %></option>
                <% end %>
  • The edit, view, and print methods simply call an internal (private) method named get_content, which reads in the request file and passes on the file attributes and its contents to the corresponding views via the @filestat and @content instance variables, respectively. The following code shows how:
    def get_content
        @filename = get_filename params[:f]
        @filestat = Wiki.attributes @filename
        @content = Wiki.find @filename
        rescue Errno::ENOENT => exception
             flash[:error] = exception
             redirect_to :action => :index
  • Apart from reading in the contents and attributes of the requested file, the get_content method also redirects the page to your index page, along with an error message in case of an exception. The error message is set using the Rails flash feature (essentially, a hash containing application errors).

    Once the controller has passed control to the view, the view simply displays contents of the instance variable @content. For example, the view.rhtml template has a single line of code in the entire file that not only displays the content of the Wiki file but also converts it prior to doing so using a helper method named to_html:

    <%= to_html @content %>

    The to_html helper method can be found in the app/helpers/wiki_helper.rb file, a Rails-generated helper module (helper modules are a nice way to make methods available to your views). This is also where the gem named RedCloth, discussed earlier, comes into play since it does the leg work of converting the raw Wiki file text to HTML, as shown in this code contained in our wiki_helper.rb file:

    require 'redcloth'
    module WikiHelper
      # parse and return data as HTML
      def to_html(rawtext)
        return "" if rawtext.nil?
        r = RedCloth.new rawtext

    Close Icon
    Thanks for your registration, follow us on our social networks to keep up-to-date