Ruby on rails 在post模型中保存前的未定义方法错误
我有一个post模型,我试图实现一个before_save调用,以便在将表单保存到数据库之前操作表单中的输入。我尝试的任何操作都会抛出错误,无论是downcase、parameterize、gsub还是split。我每次都会得到一个Ruby on rails 在post模型中保存前的未定义方法错误,ruby-on-rails,Ruby On Rails,我有一个post模型,我试图实现一个before_save调用,以便在将表单保存到数据库之前操作表单中的输入。我尝试的任何操作都会抛出错误,无论是downcase、parameterize、gsub还是split。我每次都会得到一个未定义的方法“downcase”,用于错误。只要用我想做的任何事情来替换掉downcase。我要做的就是接受用户输入的任何内容,并用下划线替换空格 这里是Post模型的一个版本 class Post < ApplicationRecord belongs_t
未定义的方法“downcase”,用于错误。只要用我想做的任何事情来替换掉downcase。我要做的就是接受用户输入的任何内容,并用下划线替换空格
这里是Post模型的一个版本
class Post < ApplicationRecord
belongs_to :user
has_many :comments, dependent: :destroy
before_save :permalink_tag
strip_attributes
acts_as_taggable
private
def permalink_tag
self.tag_list = self.tag_list.split(' ').join('_') unless self.tag_list.nil?
end
end
更详细的错误信息:
NoMethodError in PostsController#update
undefined method `downcase' for ["sample-tag"]:ActsAsTaggableOn::TagList
应用程序跟踪:
app/models/post.rb:4:in `block in <class:Post>'
app/controllers/posts_controller.rb:45:in `block in update'
app/controllers/posts_controller.rb:44:in `update'
app/models/post.rb:4:in'block in'
app/controllers/posts_controller.rb:45:in“block in update”
app/controllers/posts_controller.rb:44:in'update'
知道这是gem的问题,我能够在他们的文档中找到答案
ActsAsTaggableOn.force_lowercase = true
ActsAsTaggableOn.force_parameterize = true
是的,这是一块宝石。安装并运行迁移后,您可以将标记列表添加到表单中。在保存之前,我正试图处理这个表单字段。
ActsAsTaggableOn.force_lowercase = true
ActsAsTaggableOn.force_parameterize = true