0

我有以下课程:

class Message
  extend ActiveModel::Naming
  include ActiveModel::Conversion

  attr_accessor :name, :emails, :custom_content

  def initialize(attrs = {})
    attrs.each do |k, v|
      self.send "#{k}=", v
    end
  end

  def persisted?
    false
  end

  def email_list
    self.emails.split(",").collect { |email| {:email => email.delete(" ")} }
  end 
end

当我在代码中调用属性时,我总是想将 name 属性大写,而不是总是这样做。

4

1 回答 1

0

弄清楚了:

class Message
  extend ActiveModel::Naming
  include ActiveModel::Conversion

  attr_accessor :name, :emails, :custom_content

  def initialize(attrs = {})
    attrs.each do |k, v|
      self.send "#{k}=", v
    end
  end

  def persisted?
    false
  end

  #Fix is here
  def name=(s)
    @name = s.titleize
  end

  def email_list
    self.emails.split(",").collect { |email| {:email => email.delete(" ")} }
  end 
end
于 2013-02-14T19:17:36.070 回答