Daniel
Daniel

Reputation: 1755

How to display two values in text_field Ruby?

I need textfield in Rails:

<%= f.text_field :first_name, autofocus: true, placeholder: :first_name , :class => 'form-control', "disabled" => "disabled" %>

I want to display two variables in one text field:

:first_name :second_name

How to do this?

Upvotes: 0

Views: 120

Answers (2)

Alejandro Montilla
Alejandro Montilla

Reputation: 2654

You should have in you model a method that concatenates first_name and second_name:

def full_name 
  "#{self.first_name}, #{self.second_name}"
end

And then call it in you view like that:

<%= f.text_field :full_name, autofocus: true, placeholder: :first_name , :class => 'form-control', "disabled" => "disabled" %>

Upvotes: 2

ashvin
ashvin

Reputation: 2040

<%= f.text_field :first_name, value: "#{f.object.first_name} #{f.object.second_name}", autofocus: true, placeholder: :first_name , :class => 'form-control', "disabled" => "disabled" %>

Upvotes: 0

Related Questions