Skip to content

Instantly share code, notes, and snippets.

@windy
Last active September 20, 2022 03:16
Show Gist options
  • Save windy/7860660e5aa21ae63051 to your computer and use it in GitHub Desktop.
Save windy/7860660e5aa21ae63051 to your computer and use it in GitHub Desktop.
如何单独使用 Rails 的路由系统
# test for Rails 4.1.9
# file: app/models/forum_markdown.rb
class ForumMarkdown
class DummyController
def logger
RAILS_DEFAULT_LOGGER
end
def headers
{}
end
def _prefixes
end
end
def self.render(options, assigns = {})
viewer = ActionView::Base.new(File.join(Rails.root, 'app/views'), assigns, DummyController.new)
helper = Rails.application.routes.url_helpers
if options[:locals]
options[:locals][:helper] = helper
end
viewer.render options
end
end
ForumMarkdown.render file: 'xx.md.erb', locals: { video_course: video_course }
# <%= video_course.name %>
![](<%= helper.root_url + ActionController::Base.helpers.asset_path(video_course.cover_url) %>)
## 课程介绍
<%= video_course.description %>
更多请点击: [课程详情](<%= helper.video_course_url(video_course) %>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment