Custom form in Active Admin - ruby-on-rails

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!

Related

Rails, keep anchor tag in devise sign_in stored_location redirect

Problem is described well here
extract:
Given I am using a Rails app with Devise authentication
And I am registered
But I am not logged in right now
When I visit the path "/path#thing"
Then I should see the sign-in form
When I sign in
Then I should be redirected to "/path#thing"
add a hidden input in your sign in page, for me it was app/views/devise/sessions/new.html.haml
and also some javascript to populate the input:
I called the input tab_hash
= simple_form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f|
= f.input :login, :hint => true, :required => true
= f.input :password, :as => :password, :hint => false, :required => true
= f.input :tab_hash, :as => :hidden
%div.text-right
= f.button :submit, t("formtastic.actions.sign_in"), class: 'btn-primary'
.text-center
%p
= render :partial => "devise/shared/links"
:javascript
$(document).ready( function(){
$("#user_tab_hash").val(window.location.hash);
});
add attr_accessor :tab_hash to the User model
in app/controllers/application_controller.rb
add
def after_sign_in_path_for(resource)
stored = stored_location_for(resource)
if stored.present?
stored += params['user']['tab_hash'] # refs #3485 after signing in the tab was lost
end
stored || root_url
end

Get ID of parent object in ActiveAdmin / Formtastic

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

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

rails ActiveAdmin nested form has_one accepts_attributes_for formtastic issue

I am using ActiveAdmin and Rails 3.1 -- having problem understanding whether the following is a bug, or if there is some way to do it correctly that I am not understanding. I am trying to use a nested model with a has one relationship, so that I can create a page and fill out it's meta data in 1 step. --
(page has_one meta_data, accepts_nested_attributes_for meta_data)
Example 1)
in this example, when I click new page, meta data section is there but there are no input fields -- also, if I edit the record, it shows up correctly, however the fieldset is duplicated in the second section... and if I remove the f.inputs wrapping semantic_field_for (which would make sense), then it breaks completely and shows nothing in the meta data area...
form do |f|
f.inputs "Page Information" do
f.input :name
f.input :uri
f.input :view
f.input :body, :as => :text
f.input :active
end
f.inputs "Meta Data" do
f.semantic_fields_for :meta_data do |meta_form|
meta_form.inputs :title, :description, :keywords, :name => "Meta Information"
end
end
end
I understand the meta data probably isn't being instantiated, but I am not sure how I am supposed to do that in the form block? (or if I can even do it) -- The only way I am able to get this to work is by doing using a custom form, and building the meta data in the view, which looks like this
2) How I am working around it, but seems hacky
<%= semantic_form_for [:admin, #page] do |f| %>
<% #page.build_meta_data %>
<%= f.inputs :name => "Page Information" do %>
<%= f.input :name %>
<%= f.input :uri %>
<%= f.input :view %>
<%= f.input :body, :as => :text %>
<%= f.input :active %>
<% end %>
<%= f.semantic_fields_for :meta_data do |meta_form| %>
<%= meta_form.inputs :title, :description, :keywords, :name => "Meta Information" %>
<% end %>
<%= f.buttons %>
<% end %>
Thanks in advance for any help or clarification.
(note to moderators I started another thread on this but was not as clear and didn't have the workaround solution I do now yet, so if one of the questions should be deleted please delete the other)
I found a better solution for you. You can use :for option in inputs helper.
f.inputs "Meta Data", for: [:meta_data, f.object.meta_data || MetaData.new] do |meta_form|
meta_form.input :title
meta_form.input :description
meta_form.input :keywords
end
I think this might work too, but I didn't check
f.inputs :title, :desctiption, :keywords,
name: "Meta Data",
for: [:meta_data, f.object.meta_data || MetaData.new]
In rails 4, this is something that works, with a nice design
e.g.,
A customer has one account
model/customer.rb
accepts_nested_attributes_for :account
admin/customer.rb
form do |f|
f.inputs do
f.input :user, input_html: { disabled: true }
f.input :name
f.input :address
f.input :city
f.input :country, as: :string
end
f.buttons
f.inputs "Account Information", for: [:account, f.object.account] do |s|
s.input :active, as: :boolean
s.input :subscription, as: :boolean
s.input :expires_on, as: :datepicker
s.actions
end
end
controller do
def permitted_params
params.permit!
end
end
end
i was having the same problem, i worked in your hack and got it working.
i then moved <% #page.build_meta_data %> to a custom new method like this
controller do
def new
#tenant = Tenant.new
#tenant.build_tenant_configurable
end
end
hope this helps

Adding a custom input field in formtastic?

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

Resources