I have a post model where I'm trying to implement a before_save call so I can manipulate input from a form before saving it to the database. Anything I try throws an error, whether it's downcase, parameterize, gsub, or split. I get an undefined method 'downcase' for
error every time. Just replace downcase with whatever I'm trying to do. All I want to do is take whatever the user inputs and replace the spaces with an underscore.
Here is one version of the Post model.
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
Now you can simply replace the code in the permalink_tag method with any of these alternatives and it still results in an error.
self.tag_list.gsub(/\s+/, "_")
before_save { |post| post.tag_list = post.tag_list.downcase }
self.tag_list.parameterize.underscore
The error in more detail:
NoMethodError in PostsController#update
undefined method `downcase' for ["sample-tag"]:ActsAsTaggableOn::TagList
Application Trace:
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'
Knowing that this was an issue with the gem, I was able to find the answer in their docs.
ActsAsTaggableOn.force_lowercase = true
ActsAsTaggableOn.force_parameterize = true
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.