【问题标题】:Rails 4 with Reform GEM and simple_form带有改革 GEM 和 simple_form 的 Rails 4
【发布时间】:2015-03-01 20:26:00
【问题描述】:

我正在尝试使用reform gem 实现嵌套。我有三个模型:

  1. 客户(belongs_to :personal_details)
  2. 个人详情(belongs_to :title)
  3. 标题(has_many :PersonalDetail)

我正在尝试使用以下代码使用改革 gem 创建一个 CustomerForm:

/app/forms/customer_form.rb
class CustomerForm < Reform::Form
property      :national_id
validates     :national_id,
            :presence => true

property :personal do

property      :first_name
property      :title


validates     :first_name,
              :presence => true


end
end


/app/controllers/customers_controller.rb
def new
  @form = CustomerForm.new(Customer.new(personal: Personal.new))
end


/app/views/customers/new.html.haml
= simple_form_for @form, :url => customers_path do |f|
  = f.input         :national_id
  = f.simple_fields_for :personal do |builder|
    = builder.input         :first_name
    = builder.association       :title
  = f.button :submit, "Submit"

:title 属性在表单提交期间不会接收任何输入。 Title 和 PersonalDetail 之间的关系仅用于使用 Title.name 列表填充下拉列表。如果我尝试上述方法,我会收到一条错误消息,指出没有关联 :gender 存在。当我使用基于标准模型的方法尝试类似的事情时,它就像一个魅力。

有什么想法吗?我已经浏览了几次文档,但根本看不出哪里出错了。

【问题讨论】:

    标签: ruby-on-rails simple-form reform


    【解决方案1】:

    我找到了另一种方法来做到这一点。我没有使用 simple_form 的标准关联方法,而是简单地将一个集合添加到 Select Dropdown,如下所示:

    .row.padding-12-all
      = builder.input :title_id, 
                      :label_method => :name , 
                      :value_method => :id,
                      :collection => Title.all
    

    【讨论】:

      猜你喜欢
      • 2013-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-22
      • 2016-01-26
      • 2015-08-15
      • 2023-03-18
      相关资源
      最近更新 更多