我想在rails上的ruby中定义一个不可编辑的文本字段,并指定默认值。我试过了,但它没有认识到只读方法:
<%= f.text_field :email,:value=> current_user.email, :readonly=>readonly %>
答案 0 :(得分:2)
要使表单字段为只读,请像其他人指出的那样设置readonly
属性:
f.text_field :email, :value => current_user.email, :readonly => true
只是在表单上设置它是不够的,您还需要在模型层中保护此属性:
class User < ActiveRecord::Base
attr_readonly :email
end
attr_readonly
上的文档:
&#34;列为readonly的属性将用于创建新记录,但更新操作将忽略这些字段。&#34;
答案 1 :(得分:0)
您应该使用:readonly => true
而不是:readonly=>readonly
<%= f.text_field :email,:value=> current_user.email, :readonly=>true %>
它会将其设为 non-editable text_field
,并允许该文本字段的参数传递给控制器。
这是一个小的 code ,用于说明 :readonly => true
与 :disabled => true
之间的区别
答案 2 :(得分:0)
该属性应为:disabled => "disabled"