0

之前有人问这个问题,但答案是 ruby​​ on rails 3.0+

基本上,我想将我的 wiki 页面(即 .md 页面)放在我的 2.3.5 ruby​​ on rails 项目的公共文件夹中。我希望用户在键入时访问 wiki 页面的主页mysite.com/wiki(即,这将映射到/public/wiki/home.md..

我如何在 ruby​​ on rails 2.3.5 中做到这一点?(在线的路由文档信息量不是很大)

一般来说,如果由于某种原因我坚持使用 RoR 2.3.5 项目..我该去哪里获取文档?似乎官方文档仅适用于最新的 RoR 版本(即 3+)

4

1 回答 1

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.

于 2013-05-03T10:48:02.887 回答