attr_accessor vs attr_accessible

attr_accessible is used to identify attributes that are accessible by
your controller methods. This is to protect your models from being
written to by malicious users posting values that they shouldn't be
into your create and update methods. All of your fields are blank
except the one that you specified to be accessible because rails is
doing it's job :)


attr_accessible will only allow access to the attributes that you
specify, denying the rest. attr_protected will deny access to the
attributes that you specify, allowing the rest, and specifying neither
in your model will allow access to all attributes.


attr_accessor is an easy way to create read and write accessors in your
class. attr_accessor :myvar replaces the following.


def myvar
  @myvar
end


def myvar=(myvar)
  @myvar=myvar
end


另外 railscasts.com 上有关于 attr_accessible 安全相关的视频

http://railscasts.com/episodes/26-hackers-love-mass-assignment

http://railscasts.com/episodes/237-dynamic-attr-accessible

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值