4

I'm writing an application in Rails 3 with Ruby v2.0.0.

I have a helper in app/helpers/posts_helper.rb:

module PostsHelper
  def markdown(text)
    @redcarpet = Redcarpet::Markdown.new(Redcarpet::Render::HTML, {fenced_code_blocks: true}) unless @redcarpet
    @redcarpet.render text
  end
end

My Gemfile contains gem 'redcarpet', '~> 2.2' and I have run bundle install with success. However, I get this error whenever I try to load a page that uses this helper:

uninitialized constant PostsHelper::Redcarpet

What can I do to get this working? I'm baffled at this problem.

Edit:

I've also tested Redcarpet in rails console:

$ bundle exec rails console
Loading development environment (Rails 3.2.13)
irb(main):001:0> Redcarpet::Markdown.new(Redcarpet::Render::HTML).render('text *markdownified*')
=> "<p>text <em>markdownified</em></p>\n"

So it works in the console, just not in my helper (or controller, I've tried that, too).

Mike Holler
  • 945
  • 2
  • 13
  • 28

1 Answers1

2

Ruby is trying to find Redcarpet in the PostsHelper namespace. Use ::Redcarpet to raise that to the global namespace

Bryan Ash
  • 4,385
  • 3
  • 41
  • 57
  • I've changed the two references from `Redcarpet` to `::Redcarpet` and now I'm getting this: `uninitialized constant Redcarpet`. – Mike Holler May 12 '13 at 04:08
  • 1
    What happens if you execute that `@redcarpet = ...` statement in the Rails console? It works for me. – Bryan Ash May 12 '13 at 04:24
  • 2
    1) Did you restart rails server after installing redcarpet? 2) Try it without instance variables, no @. 3) add require to helper, shouldnt be needed? – house9 May 12 '13 at 04:30
  • 1
    I can't believe it. I didn't restart the server. Wow. Thank you for your wonderful common sense! – Mike Holler May 12 '13 at 04:32