如何在text_field Ruby中显示两个值?

时间:2017-03-07 11:46:28

标签: ruby-on-rails ruby ruby-on-rails-4

我需要Rails中的textfield:

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

我想在一个文本字段中显示两个变量:

:first_name :second_name

怎么做?

2 个答案:

答案 0 :(得分:2)

您应该在模型中建立一个连接first_namesecond_name的方法:

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

然后在你的视图中调用它:

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

答案 1 :(得分:0)

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