Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 411
leonel Avatar asked Jan 02 '12 18:01

leonel


1 Answers

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

like image 62
Sjors Branderhorst Avatar answered Oct 21 '22 20:10

Sjors Branderhorst