简体   繁体   中英

Rails 3. Conditionally show fields with Formtastic

I'm using ActiveAdmin and 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM