Reputation: 455
I want to define a not editable textfield in ruby on rails with a default value assigned. I tried so but it doesn't recognize the readonly method:
<%= f.text_field :email,:value=> current_user.email, :readonly=>readonly %>
Upvotes: 2
Views: 2512
Reputation: 5847
To make a form field read-only, set the readonly
attribute, like others have pointed out:
f.text_field :email, :value => current_user.email, :readonly => true
Just setting it on the form isnt enough, you need to protect this attribute in the model layer too:
class User < ActiveRecord::Base
attr_readonly :email
end
The documentation on attr_readonly
:
"Attributes listed as readonly will be used to create a new record but update operations will ignore these fields."
Upvotes: 2
Reputation: 33542
You should be using :readonly => true
not :readonly=>readonly
<%= f.text_field :email,:value=> current_user.email, :readonly=>true %>
It will make it as a non-editable text_field
and allows the params of that textfield to pass to the controller.
Here is small code which tells the differences between :readonly => true
and :disabled => true
Upvotes: 0