4.2. Just a lil bit more

Typing the adresses is not the simpliest way to browse in a website. So let’s make a small menu, won’t be big and complex because I don’t want to put some css in this howto. Open the app/helpers/application_helper.rb file and add this method inside the class :

   def menu
        result_string = "" 
        result_string << link_to("Categories", :controller => "category", :action => "list") + " :: " 
        result_string << link_to("Documents", :controller => "document", :action => "list") + " :: " 
        if (@session["author_id"])
            result_string << link_to("Logout", :controller => "login", :action => "logout") + "<br /><br />" 
        else
            result_string << link_to("Login", :controller => "login") + "<br /><br />" 
        end
        return result_string
    end

The application helper allows you to define methods that you’ll call in all the controllers views. The menu is a good one isn’t it ? So add the following line in all the views :

    <%= menu() %>
So you must have seen most of the thing you need :
  • how to create simple ror pages
  • how to list, show, edit, create items
  • how to manage simple one to n relations (has_many, belongs_to)
  • how to manage more complex relations (has_and_belongs_to_many)
    • create an item in this case
    • add a relation link, or more
    • edit the links
  • how to authenticate visitors and how to have secure methods
You can add some improvements :
  • use css
  • make real html pages
  • create a better menu helper
  • use SHA1 or MD5 encryption to store the passwords
  • upload files
  • allow multiple authors for each document, you should have to :
    • replacing belongs and has_many by has_and_belongs_to_many
    • adding a authors_documents table with columns author_id and document_id
    • add some methods and code to manage the rights of the authors

This howto is based on the code I wrote to create a small app to manage my publications and a personnal professionnal page.

Thanks

Big thanks must go to all the RoR people :
  • nextangle
  • leeo
  • noradio
  • all the others :)

The author

Thomas “ange” Riboulet is a french student who has discovered Ruby one year ago and RoR one month ago. In love with both. Contact : ange[at]librium[dot]org

Contributors