ActiveAdmin show, edit, and delete actions in custom models - ruby-on-rails

I have a rails 3 application which uses the ActiveAdmin gem.
If I do not customizes my models, 3 actions are enabled in each line of my model : show, edit, delete
But if I customizes my model, the actions disappear.
Model not customized showing the actions (users.rb) :
ActiveAdmin.register User, as: 'Users_full' do
menu :parent => 'Users'
end
Custom model not showing actions (companies.rb) :
ActiveAdmin.register Company do
index do
selectable_column
column :name
column :url
end
csv do
column :name
column :url
end
end
Is there a way to get actions in customized models ? I have already tried to add : actions :all, config.batch_actions = true and action_item to my companies.rb file but nothing change.

Add the actions, like:
index do
selectable_column
column :name
column :url
actions
end
You're defining the index page's content, and that content includes the actions-if you omit them, they won't show up.

Related

ActiveAdmin: form input without corresponding table attribute

How can I set a textfield in an ActiveAdmin form, who do not correspond to a table attribute ?
I need it to build an autocomplete behavior, to populate a list of checkboxes.
In case you want the value submitted back to your model, you can create a virtual attribute by adding attr_accessible, or explicitly define them in the model, as suggested in this answer:
def my_virtual_attr= (attributes)
#this will be evaluated when you save the form
end
def my_virtual_attr
# the return of this method will be the default value of the field
end
and you will need to add it to permit_params in the ActiveModel resource file.
In case you don't need the value submitted to the backend (needed for front-end processing for example), you can actually add any custom HTML to ActiveAdmin form, and this is an example code it:
ActiveAdmin.register MyModel do
form do |f|
f.semantic_errors # shows errors on :base
f.inputs "My Custom HTML" do
f.li "<label class='label'>Label Name</label><a class='js-anchor' href='#{link}'>Link Text</a><span></span>".html_safe
f.li "<label class='label'>Label 2 Name</label><input id='auto_complete_input'/>".html_safe
end
f.inputs "Default Form Attributes" do
f.inputs # builds an input field for every attribute
end
f.actions # adds the 'Submit' and 'Cancel' buttons
end
end
You can try to remove model prefix from the params name
ActiveAdmin.register MyModel do
form do |f|
f.input :custom, input_html: { name: 'custom' } # instead of 'my_model[custom]'
end
end

Active admin In Rails 4

I have Rails 4 app, which I want to manage with Active admin. I have 2 models,
Item
Category
Item has 2 fields, name and category_id. The Category model has a field called name. The models are related with has_many :items and belongs_to :Category.
When I try to access the admin panel in Active Admin, after configuring some aspect in Active Admin, in the model Item I have a desplegable menu with the id reference of the category like this #Category:=0x675654. What I want is to get the name of the category. How can I to do this? I don't have access to edit this view.
How about something like:
ActiveAdmin.register Item do
form do |f|
f.inputs "Item" do
f.input :category, as: :select, collection: Category.all.collect {|c| [c.name, c.id] }
end
end
end
If what you want is to show the name in the index page, you need to customize it with something like:
column 'Name' do |item|
name = Category.find(item.category_id).name
end
To simply display the name :
ActiveAdmin.register Item do
menu parent: 'My Menu'
index do
id_column
column 'Category' do |item|
item.category.name
end
end
end
If you'd like to get a link to the resource on top of that name, use auto_link :
ActiveAdmin.register Item do
menu parent: 'My Menu'
index do
id_column
column 'Category' do |item|
auto_link(item.category, item.category.name)
end
end
end
The same thing applies for Item views.

Pass a parameter to the new action in Active Admin

I have two related models, Bunny has_many BunnyData (which belongs_to Bunny). From the show page of a particular Bunny (in Active Admin), I want to create a link to make a related BunnyData. I've tried a few different ways, with no success, and am currently trying this:
sidebar :data, :only => :show do
link_to 'New Data', new_admin_bunny_datum(:bunny_id => bunny.id)
end
The link being generated ends up as something like:
.../admin/bunny_data/new?bunny_id=5
But when you go to that page, the dropdown for Bunny is set to the blank default as opposed to showing the name of Bunny with ID 5.
Thanks in advance.
Rails namespaces form fields to the data model, in this case BunnyData. For the form to be pre-filled, any fields provided must also include the namespace. As an example:
ActiveAdmin.register Post do
form do |f|
f.inputs "Post Details" do
f.input :user
f.input :title
f.input :content
end
f.actions
end
end
The fields can be pre-filled by passing a hash to the path helper.
link_to 'New Post', new_admin_post_path(:post => { :user_id => user.id })
Which would generate the following path and set the form field.
/admin/posts/new?post[user_id]=5
In the case of BunnyData, it might be slightly different due to the singular and plural forms of datum. But that can be verified by inspecting the generated HTML to find the name attribute of the inputs.

Override index columns activeadmin

So I would like to have a shortened table with batch actions.
ActiveAdmin.register User do
batch_action :acitve do |selection|
User.find(selection).each do |user|
user.active! true
end
end
filter :email
index do
column :id
column :first_name
column :last_name
column :email
column :sign_in_count
default_actions
end
end
However batch action box is greyd out. It's understanbale because nothing is selected. However when I use default index settings (no columns specification), the checkbox stays there. How can I have a default checkbox with custom columns?
according to this (Customizing Table Columns part) you need to add
index do
selectable_column #batch actions checkboxes column
column ...
...
end
to render checkboxes

Rails Craiglist like list of items

So I have a state model and city model associated with has_many and belongs_to. I want to display a page with each state with its associated cities underneath.
I created a page controller and page called "Locations" and manually entered in
<%= link_to "Allentown", allentown_path %>
which then takes you to the allentown page.
On the allentown page I filtered the listings by adding this code to the pages controller
def allentown
#title = "Allentown Listings"
#tattoo_briefs = TattooBrief.where( :city_id => 1 ).find(:all, :order => "id DESC" )
end
I know this isn't DRY. Also can get very cumberson if I have 200 cities. Is there a better way?
You need to add a resource to your routes:
routes.rb
resources :city
That essentially gives you all the RESTful actions for the City model. Then, in your controller, use the show action to..wait for it..show your city page
cities_controller.rb
def show
#city = City.find(params[:id])
#title = "#{#city.name} Listings"
#tattoo_briefs = TattooBrief.where( :city_id => params[:id] ).find(:all, :order => "id DESC" )
end
you can still modify this by studying more on routes and controllers from the rails api. With added knowledge, you can get to allentown by modifying your route to use the city name instead of the id.

Resources