0

this question was asked before, but the answer was for ruby on rails 3.0+

Basically I want to put my wiki pages (ie .md pages) inside my public folder in my 2.3.5 ruby on rails project. and I want users to access the home of the wiki page when they type mysite.com/wiki (ie this would map to /public/wiki/home.md)..

how do I do this in ruby on rails 2.3.5? (the routing documentation online wasn't very informative)

Also in general if for some reason I'm stuck with an RoR 2.3.5 project.. where do I go for documentation? It seems the official documentation only pertains to the latest RoR version (ie 3+)

Community
  • 1
  • 1
abbood
  • 23,101
  • 16
  • 132
  • 246
  • 1
    The guides for rails 2.3.x are here: http://guides.rubyonrails.org/v2.3.11/ If you're on 2.3.5, you definitely need to upgrade, there are some big security concerns in versions less than 2.3.11. – theTRON May 03 '13 at 09:58
  • @theTRON uha.. tell that to the [fedena](http://www.projectfedena.org/) folks – abbood May 03 '13 at 10:40

1 Answers1

2

I presume that you want the Markdown to be rendered. If you simply serve it up from your public directory, then Rails won't render it.

What you could do is an a new controller, say WikiController, which could render markdown files that you store somewhere like lib/wiki. I haven't tested any of this directly, so you should take it only as a guide, but it should work okay.

The controller might look like something like this:

# app/controllers/wiki_controller.rb
class WikiController < ApplicationController
  def show
    page = File.open(File.join(Rails.root, 'lib', 'wiki', "#{params[:page_id]}.md"), 'r') { |f| f.read }
    markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true)
    render :html => markdown.render(File.join(Rails.root, 'lib', 'wiki', "#{params[:page_id]}.md"))
  end
end

And you could add a route like this:

# config/routes.rb
map.connect 'wiki', :controller => 'wiki', :action => 'show', :page_id => 'home'
map.connect 'wiki/*page_id', :controller => 'wiki', :action => 'show', :as => :wiki

The first route handles your special case (home.md) and the second will allow you to structure your wiki however you like (including placing files in subdirectories, etc). Linking to /wiki/help/getting_started will try to render the file lib/wiki/help/getting_started.md.

You also have a link helper method, so that if you need to link to a wiki page from within your app you can call wiki_path(:page_id => 'help/getting_started').

This solution assumes you're using RedCarpet for Markdown rendering, however you could switch up any renderer you like.

theTRON
  • 9,608
  • 2
  • 32
  • 46