【问题标题】:Rails 3. Conditionally show fields with FormtasticRails 3. 使用 Formtastic 有条件地显示字段
【发布时间】:2012-02-01 00:10:07
【问题描述】:

我正在使用 ActiveAdmin 和 Formtastic。

我有一个发票表格,其中包含发货下拉菜单。

form do |f|
  f.inputs "Shipment Details" do      
  f.input :shipment_id, :label => "Shipment", :as => :select, :collection => Shipment.find(invoiceless_shipments, :order => "file_number", :select => "id, file_number").map{|v| [v.file_number, v.id] }
  f.input :issued_at, :label => "Date", :as => :datepicker
  ... more fields ...
end

如果表单是新发票表单,我只想显示发货选择菜单。

如果表单是编辑表单,我不想显示发货下拉选择菜单。所以如果表单是编辑表单,它不会被改变。

我正在考虑做类似的事情

if params[:action] != 'edit'
  f.input :shipment_id, :label => "Shipment", :as => :select...
end

但我收到了 DSL 错误。

【问题讨论】:

  • 别想了,unless f.object.persisted?unless f.object.persisted?
  • 遇到了同样的问题,将字段包含在 if object.persisted? 块中有所帮助。谢谢。

标签: ruby-on-rails ruby formtastic activeadmin


【解决方案1】:

试试

form do |f|
  f.inputs "Shipment Details" do      
    if f.object.new_record?
        f.input :shipment_id, :label => "Shipment", :as => :select...
    end
    ...
  end
end

问题(部分)在这里早先回答:Accessing object of form in formtastic

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多