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
Related
I have the relation ContentRelease has_many pages. The following activeadmin registered model lets me edit properties of a page inside the content_release, but not create new pages for the content release. There is no error, I get a success message, but a new page is not saved. What am I doing wrong?
ActiveAdmin.register ContentRelease do
form do |f|
f.actions
f.inputs "Details" do
f.input :active_at
end
f.inputs "Pages", :class => 'pages-list' do
f.has_many :pages do |page|
page.inputs "Associated Look" do
page.input :title
page.input :content_release, :as => :select, :collection => ContentRelease.list
page.input :look, :as => :select, :collection => Look.current
page.input :share_url
end
end
end
f.inputs "Looks", :class => 'looks-list' do
f.has_many :looks do |look|
look.inputs do
look.input :title
end
end
end
f.actions
end
I am facing an issue showing up the error messages in active admin.
I get all the error messages displayed with the fields in the form.
But in the code below, I need atleast one skill and maximum 5 skills to be added.
Else need to throw an error message.
I've added a validation in model as :
validates :skills, :length => { :minimum => 1, :maximum => 5,
:message => " should be atleast 1 and less than 5"}
This validates perfectly, but no error message is displayed.
Can anyone help me with the display of the error message.
Following is the code :
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "User", :multipart => true do
f.input :name
f.input :email, :as => :email
f.input :profile_name
f.input :date_of_birth
f.input :gender, :as => :select, :collection => Gender::GENDERS
end
f.inputs "Skills* ( minimum 1 & maximum 5 )" do
f.has_many :skills do |p|
if !p.object.nil?
# show the destroy checkbox only if it is an existing appointment
# else, there's already dynamic JS to add / remove new appointments
p.input :_destroy, :as => :boolean, :label => "Destroy?",
:hint => "Check this checkbox, if you want to delete this field."
end
p.input :description
p.input :title
end
end
end
end
activeadmin 0.5.1 is available on github.
it contains next line in changelog
"Add support for semantic errors #905 by #robdiciuccio"
here is pull request with this feature
https://github.com/gregbell/active_admin/pull/905
example
form do |f|
f.semantic_errors *f.object.errors.keys
f.inputs
f.inputs "Locations" do
f.has_many :locations do |loc|
loc.input :address
loc.input :_destroy, :as => :boolean, :label => "Delete"
end
end
f.buttons
end
to use it add to Gemfile
gem 'activeadmin', :git => "git://github.com/gregbell/active_admin.git", :tag => "v0.5.1"
For passing validation try this
validates_length_of :skills,
:within => 1..5,
:too_short => 'too short message',
:too_long => 'too long message'
I am creating a custom form in Active Admin 0.5. I have registered a page and created a form through the DSL:
ActiveAdmin.register_page 'Planning', :namespace => :pos_admin do
content :title => proc{ I18n.t("active_admin.dashboard") } do
form do |f|
f.input :type => :text
f.input :type => :submit
end
end
end
The problem is that when submitting the form I get an empty Params hash. And the form tag contains no authenticity token.
What am I doing wrong?
An old post, but for anyone stumbling upon this issue, the answer is to add
f.input :name => 'authenticity_token', :type => :hidden, :value => form_authenticity_token.to_s
to the form. This passes the auth token back to ActiveAdmin so that it can confirm no forgery has taken place. Your session was being terminated and you were taken back to the login screen because ActiveAdmin thought you were trying to forge a submission.
Your form should now look like this
form do |f|
f.input :name => 'authenticity_token', :type => :hidden, :value => form_authenticity_token.to_s
f.input :type => :text
f.input :type => :submit
end
I use next syntax with AA forms (with f.inputs do block)
Also you have to use property names of object for inputs
form do |f|
f.inputs do
f.input :property_name, :type => :text
end
f.actions
end
Hope it will help!
I have a resource Photos, which belongs to Adverts.
In ActiveAdmin, users should be able to upload Photos directly from the Advert's edit page (obviously only once the advert has been created).
The form is generated as follows:
form do |f|
[... the usual forms ...]
f.inputs "Photos" do
f.has_many :photos, :title => "Photo" do |p|
p.input :advert, :as => :hidden, :value => Advert.find(params[:id])
p.input :title
p.input :image
end
end
end
I would like the line
p.input :advert, :as => :hidden, :value => Advert.find(params[:id])
to produce a hidden field with the ID of the Advert the user is editing, however this just produces an empty field. I've tried a number of other options as well, but can't seem to figure it out.
Any hints?
You can use advert variable, which holds Advert object with id from params.
p.input :advert, :as => :hidden, :value => advert.id
by the way, your code is invalid. You get whole Advert object, not just id. Valid code:
p.input :advert, :as => :hidden, :value => Advert.find(params[:id]).id
this should works, too
I can't figure out, or find any solutions to a very simple question:
"How can I define my own input field in formtastic?"
This is what I got:
<%= semantic_form_for #someFantasticVariable, :url => "/someFantasticUrl.html" do |f|%>
<%= f.inputs do %>
<%= f.input :something_else_id, :required => true , :as => :select, :collection => SomethingElse.find(:all), :label =>"The something else"%>
<%= f.input :fantastic_max_cost, :label => "Budget (max cost)"%>
<%end%>
<%= f.buttons do%>
<%= f.commit_button :button_html => { :class => "primary", :disable_with => 'Processing...', :id => "commitButton"}%>
<%end%>
<%end%>
Now..
I want to have a very simple thing. I want to ad a field that is not part of the model. I want to have a date field that I can use to calculate some stuff in my controller. So I want to do this:
<%= f.inputs do %>
<%= f.input :something_else_id, :required => true , :as => :select, :collection => SomethingElse.find(:all), :label =>"The something else"%>
<%= f.input :fantastic_max_cost, :label => "Budget (max cost)"%>
<%= f.input :start_date, :as => :date , :label => "Start date"%>
<%end%>
But apparetly I'm not allowed, and I can't find any way to do this through my thrusted googling. Any help / ideas?
If you have some attribute that is not part of your model, then a getter and a setter should exist on the model:
def start_date
end
def start_date=(arg)
end
Then you can calculate your staff on a controller or whatever you want:
...
puts params[:somefantasticvariable][:start_date]
...
But this is a quick formtastic hack, you should find some better way, like non-formtastic input with some css etc.
Ruby provides a database-less construct called an attr_accessor. It is the equivalent of writing setter and getter methods. Formtastic will see this attribute similar to a database-backed attribute.
In your #someFantasticVariable model:
attr_accessor :start_date
If using attr_accessible in your #someFantasticVariable model, be sure to add the start_date variable there too:
attr_accessible :start_date
Because the attribute is type-less, Formtastic cannot derive the HTML input field to use. You will need to manually set the input type using :as. For your example:
<%= f.input :start_date, :as => :date_select %>
Cite:
http://apidock.com/ruby/Module/attr_accessor
https://github.com/justinfrench/formtastic#the-available-inputs