Rails attr_readonly 不起作用 [英] Rails attr_readonly doesn't work
本文介绍了Rails attr_readonly 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
根据这个问题和文档 attr_readonly
以下应该是可能的:
According to this question and the documentation of attr_readonly
the following should be possible:
class MyModel < ActiveRecord::Base
attr_accessible :foo
attr_readonly :bar
end
m = MyModel.create(foo: '123', bar: 'bar') # Should work
m.update_attributes(bar: 'baz') # Should not work
但是第一个失败了,说我不能批量分配bar
.我错过了什么?
However the first one fails, saying that I can't mass-assign bar
. What am I mising?
推荐答案
来自文档一个>
attr_accessible
获取可访问的属性列表.所有其他属性都将受到保护.
attr_accessible
takes a list of attributes that will be accessible. All other attributes will be protected.
所以 attr_accessible
将 bar
属性设置为防止批量赋值.
So attr_accessible
made bar
attribute as protected from mass-assignment.
这篇关于Rails attr_readonly 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文