How to set value of current logged-in user - ruby-on-rails

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

Related

Unable to click radio button in Ruby ActiveAdmin

I am displaying a form using ruby on rails as follows.
form do |f|
f.semantic_errors *f.object.errors.keys
# Form creation
f.inputs "User" do
f.input :first_name
f.input :last_name
f.input :email
f.input :phone_number, required: false, as: :number
f.input :password
f.input :text_sms, as: :radio, :label => "Receive sms", :checked => "Yes"
end
f.action
end
This displays the form correctly, but the problem exists when I click the radio button. I am unable to select the radio button.
By default Yes is selected, but when I click on No, it doesn't allow me.
Any help will be appreciated.
Rails version : 4.2.0
There is way, Try to replace your with
f.input :text_sms, as: :radio, :label => "Receive sms",:collection => [ ['Yes','yes',{:checked => true}], ['No','no'] ]
As given below:
form do |f|
f.semantic_errors *f.object.errors.keys
# Form creation
f.inputs "User" do
f.input :first_name
f.input :last_name
f.input :email
f.input :phone_number, required: false, as: :number
f.input :password
f.input :text_sms, as: :radio, :label => "Receive sms",:collection => [ ['Yes','yes',{:checked => true}], ['No','no'] ]
end
f.action
end

Nested Form does not Display form for ActiveAdmin

I am current using activeadmin to create a nested form. I can click on the add nested resource button which will display the inputs for the respective nested resources. However, I would like to have the inputs display by default without having to click on the add resource button (i.e the inputs should be displayed once the form is loaded).
I have rechecked my code against the ActiveAdmin docs and also checked various stackoverflow posts but was unable to make much progress.
Picture of how the form current looks like
My code is as follows in admin/project.rb:
form title: 'Create a New Project' do |f|
f.inputs 'Basic Information' do
f.input :category
f.input :name
f.input :location
f.input :size, sortable: :size do |project|
"#{project.size}m2"
end
f.input :published?
f.has_many :project_main_image, heading: 'Project main image', allow_destroy: true, new_record: false do |a|
a.input :photo, hint: image_tag(a.object.photo.thumb.url, class: 'active-admin-thumbnail')
a.input :orientation
end
f.has_many :project_descriptions, allow_destroy: true do |a|
a.input :contents, as: :ckeditor, input_html: { ckeditor: { toolbar: 'Full' } }
end
f.has_many :project_gallery_images, allow_destroy: true do |a|
a.input :photo, hint: image_tag(a.object.photo.thumb.url, class: 'active-admin-thumbnail')
a.input :orientation
a.input :row_order
end
f.actions
end
end
Any feedback or advice. Do let me know if you require more information.
That's what something i did.
you can take nested attributes like this with for keyword:
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
Hope this is helpful.

Rails ActiveAdmin. How to set default value?

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}"} %>

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

Resources