Rails 4 + ActiveAdmin: Attribute limited to a few values -- customizing ActiveAdmin form based on this? - ruby-on-rails

So I have a CareerEntry model that has a fullintern attribute, which is a string that is supposed to specify whether the entry represents an internship or a full-time position. I limit the values that can appear in this attribute as follows:
validates_inclusion_of :fullintern, :in => ["Internship", "Full-time"]
However, in ActiveAdmin, the part in the edit form that deals with the fullintern attribute still has a text field. How do I make it a dropdown box where the admin can select either "Internship" or "Full-time"?
Thanks.

You can use Formtastic's input helpers to use a select input:
form do |f|
f.inputs "Details" do
f.input :fullintern, as: :select, collection: ["Internship", "Full-time"]
end
f.actions
end
See Formtastic's usage section for the full set of native capabilities.

Related

ActiveAdmin: display parameter of object selected in form

I am not very familiar with ActiveAdmin or rails in general, so sorry if I use some incorrect phrasing.
I have a model for an Athlete, which has an attribute "notes". This is described in the permit_params of the athlete.rb.
On an additional page, I have the following:
f.input :athlete, :collection => Athlete.all.sort_by(&:last_name), :required => true
I would like to find a way to, if the :notes is not empty, display it.
If I could display it as a "row" on the form, that would be great.
Thanks!

Adding description paragraph to ActiveAdmin /new Page

I have a page set up on ActiveAdmin and when the user clicks the "Create One" button, I want to add a short textbox on the /new page (either at the top or part of the actual form, whatever's easier) explaining what needs to be put into the form.
How can I do this?
Active Admin gives complete control over the output of the form by creating a thin DSL on top of the fabulous DSL created by Formtastic (http://github.com/justinfrench/formtastic).
And you can add :hint for each form input like this:
form do |f|
f.inputs 'Details' do
f.input :title, :required => true, :hint => "This field should be filled in"
end
f.inputs 'Advanced' do
f.input :keywords, :hint => "Example: ruby, rails, active-admin"
...
end
end
Take a look Formtastic documentation, there is lot of capabilities...
You can simply add new description column in your model or you can use active admin comments here is comments description for active admin.
Hope it would help you.

Manipulating tags with acts_as_taggable_on and ActiveAdmin

I have a Post model which I'm accessing through ActiveAdmin. It's also taggable using the acts_as_taggable_on gem. So the admin can add, edit or delete tags from a specific Post.
The normal way to add the tagging functionality for the resource in your admin panel is by doing this in admin/posts.rb:
ActiveAdmin.register Post do
form do |f|
f.inputs "Details", :multipart => true do
f.input :tag_list
# and the other irrelevant fields goes here
end
f.buttons
end
end
However, I want to have the tags selected from a multiple select form field and not being entered manually in a text field (like it is with the code above). So I've tried doing this:
f.input :tag_list, :as => :select,
:multiple => :true,
:collection => ActsAsTaggableOn::Tag.all
but it doesn't work as expected. This actually creates new tags with some integer values for names and assigns them to that Post. Someone told me that extra code is needed for this to work.
Any clues on how this is done? Here's my model just in case: http://pastie.org/3911123
Thanks in advance.
Instead of
:collection => ActsAsTaggableOn::Tag.all
try
:collection => ActsAsTaggableOn::Tag.pluck(:name)
Setting the collection to Tag.all is going to tag your posts with the tag's ID, since that's how tags are identified by default (that's where the integer values for names are coming from). map(&:name) tells the form builder to use the tag's name instead.

How to pass an array in a variable for :collection in form for rails?

Here is a simple_form code in _form.html.erb.
<%= f.input :start_time, :label => "Start Timeļ¼š", :collection => #time_slot %>
#time_slot is a variable defined in the controller. It is an array and looks like:
#time_slot = ['00:30 AM','01:00 AM','01:30 AM','02:00 AM','02:30 AM','03:00 AM' ,'03:30 AM','04:00 AM','04:30 AM']
The problem is that the rendered view does not have the dropdown menu with the predefined time slots listed. Instead it only shows a text box.
How to show the drop down time slots instead of a text box? Thanks.
Firstly: input was deprecated in v2.3.8, so if you're using Rails3 you should probably not be using input. Instead, use the proper form helpers, for a drop-down box (or select tag) you'd want:
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select
but if you must - you can probably pass the input-type eg :type => :select but I've never tried that and it may not work.

simple_form input with multiple fields

I'm not quite sure what the correct terms are, but what I'm trying to do is in a form (preferably using the simple_form gem) have one of the inputs, :maximum, use both a text field and select box. The user would type in the text box a number, and then select from a dropdown box of hours, days, or months. So 21 days, 3 months, 3 hours, etc. When the form was submitted I would convert that to days and store it in the database. I know how to change the input type in simple_form, but is it possible to have two inputs for one variable?
Sure :) Here is my idea:
First, you define accessors in your user model:
attr_accessor :thing, :another_thing, :and_another_thing
Then in your view, 'inside' form_for helper, you could write for example:
<%= form.input :thing, :as => :boolean %>
<%= form.input :another_thing, :as => :text %>
...or whatever you want. (Note: I am using formtastic here. You should consider using Rails methods if you're not using formtastic gem. )
Finally, you define a callback in you user model:
before_create :build_my_fancy_record
def build_my_fancy_record
self.storage_field = "#{thing} #{another_thing}"
end

Resources