模型中的酉常数
我有一个使用Mongoid的非常简单的模型。 我已经添加了使用Redcarpet解析MD并存储它。 但是在update_attributes期间,它会抛出exception。 运行模型并通过rails c
运行更新工作正常。
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
这是控制器爆炸了。
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
这是exception和堆栈跟踪。 由于我从模型中删除了东西,因此行号略有偏差。
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'
如果重要的话,我正在运行MRI 1.9.2-p290和Rails 3.1-rc5。
编辑 – 这一切在运行测试和通过控制台运行时都能正常工作。 但是,通过控制器更新/创建模型似乎总是失败。 此外,从堆栈跟踪中,您可以看到模型位于标准位置。
您可能缺少require
或gem
声明,具体取决于您使用Redcarpet
。
如果在app/models
等标准位置定义Rails自动加载器通常会捕获这些,或者可选的lib/
。
通常,您可以通过在config/initializers/redcarpet.rb
类型文件中放入相应的require
语句或根据需要更改Gemspec
来解决此问题。
您可以尝试将Redcarpet.new
更改为::Redcarpet.new
,这将告诉Ruby寻找顶级常量Redcarpet
。 我认为这可能会解决它,但问题可能更复杂。
- 在Ruby中,没有办法在当前上下文中动态定义局部变量吗?
- validation失败时渲染/重定向到新操作(rails)
- 重新加载表单时,嵌套输入消失
- Hpricot – UTF-8问题
- / usr / bin / env ruby_noexec_wrapper失败,没有文件或目录
- Ruby Devise,SessionsController.create,json – 获取NameError:undefined’build_resource’?
- 如何从ActiveRecord中获取每组的最新记录?
- 如何在RubyMine中运行’gem install bundler’?
- 为什么#!/ usr / bin / env ruby在crontab中不起作用?