【问题标题】:Display full name in rails select form defined in model在模型中定义的 Rails 选择表单中显示全名
【发布时间】:2019-11-21 15:53:06
【问题描述】:

我正在开发 rails 5 应用程序。我面临的问题是我正在定义一个下拉列表以从博客表单中的另一个表中选择用户。关联如下。

blog.rb

has_many :blogs

def set_full_name
 self.full_name = [first_name, last_name].join(' ')
end

用户.rb

belongs_to :user

问题出在这个选择表单中,而不是只显示下拉列表中的名字,我要显示我在用户模型中定义的用户的全名。我该怎么做?

<%= form.collection_select :user_id, User.all, :id, :first_name, {prompt: "Select"}, autofocus:true, class: "form-control", id: "user" %>

【问题讨论】:

    标签: ruby-on-rails-5 actionview


    【解决方案1】:

    您正在为 text 属性调用 :first_name。使用:full_name

    &lt;%= form.collection_select :user_id, User.all, :id, :full_name, {prompt: "Select"}, autofocus:true, class: "form-control", id: "user" %&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-21
      • 2017-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多