Rails ActiveAdmin. How to set default value? - ruby-on-rails

I have code like this:
ActiveAdmin.register Post do
form do |f|
f.inputs "Post Details" do
f.input :title
f.input :body
f.input :published_at, :as => DateTime.now
end
f.actions
end
I want the field :published_at (which is t.datetime) to be set to the current date and time by default. My example doesn't work. How can I achieve this?

Yep. Found the answer myself.
ActiveAdmin.register Post do
form do |f|
f.object.published_at = DateTime.now
f.inputs "Post Details" do
f.input :title
f.input :body
f.input :published_at
...
end
end

You can try with something like this:
<%= f.input :published_at, input_html: {value: "#{Time.now}"} %>

Related

How to set value of current logged-in user

I'm creating an active admin form where I want to set the value of "admin_user_id" to be the current user logging in and want to hide this as we don't need to show while filling the form. here is my form for active admin:
form do |f|
f.inputs do
f.input :admin_user_id, input_html: { value: current_admin_user }
f.input :title
f.input :description
end
f.actions
end
I resolved the issue by replacing "current_admin_user" with "current_admin_user.id".
form do |f|
f.inputs do
f.input :admin_user_id, input_html: { value: current_admin_user.id }
f.input :title
f.input :description
end
f.actions
end

View based ActiveAdmin item displaying errors

I have my workers view based on users table, and I need separate item in my ActiveAdmin, so I changed default create/update logic to sth like this:
def update
if params[:worker][:password].blank?
params[:worker].delete("password")
end
#user = User.find(params[:id])
if #user.update_attributes(permitted_params[:worker])
redirect_to "/admin/workers/#{#user.id}"
else
render 'new'
end
end
But when I get the errors on my validation, I can't see any errors on page, just rendering 'new'. I have tried to something like this
resource = #user
But it didn't help.
UPDATE:
My form looks like this:
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Details" do
f.input :email, as: :email
f.input :first_name
f.input :last_name
f.input :street_address
f.input :apt
f.input :zip_id, :input_html => {:class => 'zip_selector'}
f.input :phone_number
f.input :is_active
f.input :description
f.input :password, :value => ''
end
f.actions
end
Any ideas ?

Conditional Show/Hide in Active Admin

I have this form in Active Admin:
form(:html => { :multipart => true }) do |f|
f.inputs 'Home Carousel Image' do
f.input :name
f.input :file, as: :file
f.input :headline_text, as: :html_editor
f.input :button_text
f.input :featured_image?
f.input :headline_text
f.input :button_text
end
actions
end
featured_image? is a boolean. I was hoping to see if a user selects this (switching it to true), only then would the input fields for :headline_text and :button_text be displayed. Otherwise, these two fields will be hidden on the form.
Is this possible?
Thanks
Yes, just use if:
f.input :featured_image?
if f.object.featured_image?
f.input :headline_text
f.input :button_text
end
Use f.object to take instance of your model.

Add two values to a form select box

I want to include BOTH manager first name and last name in a select box below. How can I accomplish this?
Form:
<%= simple_form_for #office do |f| %>
<%= f.input :street_address %>
<%= f.input :city %>
<%= f.input :postal_code %>
<%= f.input :description, as: :text %>
<%=f.input :manager_id, collection: Manager.all, :id, :last_name, include_blank: true %>
<%= f.submit 'Add Office' %>
You can add something like
label_method: lambda { |manager| "#{manager.first_name} #{manager.last_name}" }
to your f.input
or you can create a new method "name" in your model, and use this one instead
def name
"#{first_name} #{last_name}"
end
then
label_method: :name

Automatic Formbuilder for Mongoid 2.0 ? Rails

Is there a formbuilder for Mongoid 2.0 ?
Which generates automatically a form from a model.
Thanks
Why not just use Rails sacffolding?
https://github.com/mcasimir/document_form
gem document_form
It's a fork from https://github.com/justinfrench/formtastic i've made, just moved to Mongoid 2.
Model
class Person
include Mongoid::Document
include Mongoid::MultiParameterAttributes
validates_presence_of :name
field :name
field :secret, :private => true
field :birthday, :type => Date
field :department_number, :type => Integer, :range => 1..10
field :description, :long => true
end
View
<% document_form_for #object do |f| %>
<%= f.inputs %>
<%= f.buttons %>
<% end %>
This is a basic example: here the form builder will render the fields in the same order they are declared, skipping those who have :private => true.
If you are not in a hurry and you want something more flexible you can always specify fields ad options using the same syntax as formtastic, something like this:
<% f.inputs do %>
<%= f.input :title %>
<%= f.input :published, :label => "This post is published" %>
<%= f.input :section_id %>
<%= f.input :image_filename, :hint => "540x300" %>
<% end %>
If you'll decide to give it a try it i will appreciate any sort of feedback.

Resources