Search code examples
ruby-on-railsrubyformtasticactiveadmin

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.


Solution

  • 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