Seba92
Seba92

Reputation: 455

Not editable textfield in ruby on rails

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

Answers (3)

Arctodus
Arctodus

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

nickcen
nickcen

Reputation: 1692

the attribute should be :disabled => "disabled"

Upvotes: 0

Pavan
Pavan

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

Related Questions