【问题标题】:Rails simple_form associationRails simple_form 关联
【发布时间】:2012-04-09 03:43:21
【问题描述】:

我正在尝试使用 simple_form 为“成员”创建一个表单,但在显示关联时遇到了问题,它显示的组织如下所示,而不是 id 或组织名称。我在这里错过了什么吗?我该怎么办?

**组织:0x0000000485cf88

组织:0x0000000485c948

组织:0x0000000485c358**

class Organization < ActiveRecord::Base
  has_many :members
  attr_accessible :organization_name
end

class Member < ActiveRecord::Base
  belongs_to :organization
  attr_accessible :active, :email, :first_name, :last_name, :role
end

  <%= f.input :first_name %>
  <%= f.input :last_name %>
  <%= f.input :role %>
  <%= f.input :email %>
  <%= f.input :active %>
  <%= f.association :organization %>

  <%= f.button :submit %>

谢谢。

干杯, 阿兹伦

【问题讨论】:

  • 你能把members_controllernew动作和整个表格展示出来吗?
  • 看起来组织模型没有以下任何字段:[ :to_label, :name, :title, :to_s ] 所以SimpleForm 无法检测到用于收集的默认标签和值方法。我认为你应该手动传递它。
  • 使用 :to_label 方法解决。谢谢。
  • 顺便记录一下,我在成员迁移中使用了组织:引用,如果我只使用组织 ID:整数,这将是我使用 f.association 所期望的:组织。无论如何感谢你们的帮助。欣赏。

标签: ruby-on-rails simple-form


【解决方案1】:

看起来Organization 模型没有以下任何字段:[ :to_label, :name, :title, :to_s ] 所以SimpleForm 无法检测到用于收集的默认标签和值方法。我认为你应该手动传递它。

【讨论】:

    【解决方案2】:

    to_label 函数添加到您的组织类中,如下所示

    class Organization < ActiveRecord::Base
      has_many :members
      attr_accessible :organization_name
    
      def to_label
        "#{organization_name}"
    
      end
    end
    

    参考 Simple form association custom label name

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多