我有一个模型,用户在初始创建后不允许更新大多数字段。
我已经看到了 :readonly
HTML 属性,我可以在所有字段帮助器上添加,但是在所有字段上执行条件感觉......很恶心。
目前我没有使用任何特殊的东西来创建我的表单,只是简单的 HAML。有人知道更好的方法吗?
这就是我到目前为止要做的事情:
def set_readonly?(object, html_attr)
html_attr.merge(object.new_record? ? {} : {:readonly => 'readonly'})
end
用作:
f.text_field :supplier_id, set_readonly?(@damaged_goods, {:size => 5})
让我流口水的解决方案将是一种将属性与状态机 然后将传播到视图。 :)
I've a model that the user isn't allowed to update most fields on after the initial creation.
I've seen the :readonly
HTML attribute I can tack on all field helpers, but doing conditionals on all fields feels... icky.
I'm not using anything special for creating my forms at the moment, just plain HAML. Anyone know of a better way of doing this?
This is what I've thought of doing it so far:
def set_readonly?(object, html_attr)
html_attr.merge(object.new_record? ? {} : {:readonly => 'readonly'})
end
Used as:
f.text_field :supplier_id, set_readonly?(@damaged_goods, {:size => 5})
The solution to make me drool would be a way to set an attribute as read-only on the model together with State Machine which then would propagate to the views. :)
发布评论
评论(2)
以下是禁用
字段的一种方法,而无需在
new
和edit
视图中复制表单助手:Here's one way to disable an
<INPUT>
field without duplicating your form helper in yournew
andedit
views:使用
attr_protected
(来自 比尔·艾森豪尔)。第一个结果来自谷歌搜索
rails Constants
。Use
attr_protected
(from Bill Eisenhauer).1st result from Googling
rails constants
.