【问题标题】:Rails/Formtastic Newbie: Setting display field for referenced "select" boxRails/Formtastic Newbie:为引用的“选择”框设置显示字段
【发布时间】:2011-03-21 05:36:10
【问题描述】:

这里有新的formtastic 用户。

我有一个关系user has_many clients

formtastic 中,如果我这样做

f.input :employer

它返回一个包含所有雇主对象引用的选择框。我想改为显示(姓氏,名字)。我确信这很简单,但我不知道该怎么做。任何帮助表示赞赏。

【问题讨论】:

    标签: ruby-on-rails formtastic


    【解决方案1】:

    这些对我没有用,但是,它确实有效:

    <%= f.input :user, :label => "Author", :label_method => :username %> 
    

    也干净一点^^

    或者,您可以在模型本身上一劳永逸地设置显示方法:

    (在雇主.rb 中)

    def to_label
      email
    end
    

    【讨论】:

      【解决方案2】:

      试试

      f.input :employers, :as =&gt; :select, :collection =&gt; Employer.find(:all, :order =&gt; "last_name ASC")

      f.input :employers, :as =&gt; :select, :collection =&gt; Employer.collect {|e| [e.last_name, e.id] }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-02-01
        • 1970-01-01
        • 2015-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多