Rails 3.有条件地使用Formtastic显示字段

时间:2022-01-27 15:57:43

I'm using ActiveAdmin and Formtastic.

我正在使用ActiveAdmin和Formtastic。

I have an invoice form that has a drop down menu of shipments.

我有一张发票表单,上面有一份下载菜单。

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

I only want to display the select menu for shipments if the form is a New Invoice form.

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

I do not want to display the shipments drop down select menu if the form is an edit form. So if the form is an edit form, it won't be changed.

如果表单是编辑表单,我不想显示发货下拉选择菜单。因此,如果表单是编辑表单,则不会更改。

I was thinking about doing something like

我在考虑做类似的事情

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

but I get a DSL error.

但我收到DSL错误。

1 个解决方案

#1


12  

try

尝试

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

Question (partially) answered earlier here: Accessing object of form in formtastic

问题(部分)在此前回答:在formtastic中访问表单对象

#1


12  

try

尝试

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

Question (partially) answered earlier here: Accessing object of form in formtastic

问题(部分)在此前回答:在formtastic中访问表单对象