Rails的attr_readonly不起作用 [英] Rails attr_readonly doesn't work

查看:231
本文介绍了Rails的attr_readonly不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据<一href="http://stackoverflow.com/questions/7376659/make-attributes-mass-assignable-only-during-creation/7376686">this问题并的文档 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

不过第一个失败了,说我不能大规模分配。我是什么mising?

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 为防止大规模分配上做出属性。

So attr_accessible made bar attribute as protected from mass-assignment.

这篇关于Rails的attr_readonly不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆