I have a very simple model using Mongoid. I've added the use of Redcarpet to parse the MD and store it. However during update_attributes it is throwing an exception. Running the model and running the update through rails c
works fine.
class Post
include Mongoid::Document
field :contents_markdown
field :contents
key :title
before_create :markdown
before_save :markdown
protected
def markdown
if self.contents_markdown
self.contents = Redcarpet.new(self.contents_markdown).to_html.html_safe
end
end
end
Here is the controller that blows up.
def update
@post = Post.find(params[:id])
respond_to do |format|
if @post.update_attributes(params[:post])
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
Here is the exception and stacktrace. The line numbers will be slightly off as I have removed stuff from the model.
uninitialized constant Post::Redcarpet
app/models/post.rb:20:in `markdown'
app/controllers/posts_controller.rb:62:in `block in update'
app/controllers/posts_controller.rb:61:in `update'
If it matters, I'm running MRI 1.9.2-p290 and Rails 3.1-rc5.
Edit - This all works fine when running tests and running through the console. However going through the controller to update/create the model seems to always fail. Additionally from the stacktrace, you can see the model is in the standard location.