我应该如何使用Datamapper保护Sinatra app中的质量分配?

时间:2013-04-24 15:34:43

标签: ruby sinatra mass-assignment ruby-datamapper attr-accessible

我在Sinatra app中有Link模型

class Link
  include DataMapper::Resource
  has n, :views

  validates_presence_of :url,
    message: "You must specify a URL."
  validates_length_of   :url,
    maximum: 4096,
    allow_blank: true,
    message: "That URL is too long."
  validates_format_of :url,
    with: %r{^(https?|ftp)://.+}i,
    allow_blank: true,
    message: "The URL must start with http://, https://, or ftp:// ."

  property :id,         Serial
  property :url,        String
  property :token,      String
  property :created_at, DateTime
end

如何设置attr_accessible :url, :token之类的内容?

1 个答案:

答案 0 :(得分:0)

您可以使用gem DataMapper::MassAssignmentSecurity中的dm-rails模块。

class Link

  include DataMapper::Resource
  include DataMapper::MassAssignmentSecurity
  attr_accessible :url, :token

  # ...

end