Active Admin Associations - ruby-on-rails-4

Active Admin has an opinionated way of showing associations that works excellent as long as the association has a field called name. I know there's a way to tell Active Admin which field to show from the associated file but I cannot find it anywhere in the documentation.
I have a model called app_label_translation that belongs_to app_label. app_label has a field called label that I'd like Active Admin treat as it would a field named name.
As a work-around I'm doing this:
index do
selectable_column
id_column
column :app_label, sortable: "app_labels.label" do |a|
link_to a.app_label.label admin_app_label_path(a.app_label)
end
end
Does anyone know the command to override name with a field of your choice?
Update: I'm using a slightly better work-around now by having alias_attribute :name, :label in my app_label model. This allows active admin to do its thing with label. I still think there's a better way to do this.

Just pass in another argument before the model you want to display in the column.
Taken from their documentation: http://activeadmin.info/docs/3-index-pages/index-as-table.html
index do
selectable_column
column "My Custom Title", :title
end
That should do the trick.

Related

Rails 4/ActiveAdmin: Add link to list all objects from has_many relationship

I am using a cached_counter to keep track of all comments for a user.
My model relationship look like this:
class User < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
comment belongs to user, counter_cache: true
end
and the user table holds the counting variable.
In Activeadmin i have a column :comments_count which shows the amount of comments for each user.
So far so good.
Now i would like to modify it a bit. I would like to add a link which directs to a page where all comments are listed. How can do this?
I am checking the AA [live demo][1] since they do something similar there.
My idea was to create a partial view and link_to it. But i m struggling implementing it and i dont know if this is best practise anyways.
I have this query
Comments.where(:User_id => :id)
but how to i embed it into a column?
Thank for any advice .
You can handle this situation by linking to the admin index page for Comment with a filter pre-set for the given user_id. This requires drastically less code then a custom page, and gives you direct access to scopes, filters, etc.
Here's how:
index do
# make sure you set sortable, so you can click to sort!
column :comments_count, sortable: 'users.comments_count' do |user|
link_to user.comments_count,
admin_comments_path(q: { user_id_eq: user.id })
end
end
Edit:
The ActiveAdmin index controller uses Ransack to handle searching and filtering. Ransack accepts query options in the form of a hash that obeys a sort-of DSL (the user_id_eq bit above is an example). Now if you open any ActiveAdmin index route and start playing around with filters, you'll see those parameters tacked on to the end of the url using the same convention. The ?q=... part gets passed directly to Ransack in the index controller, and that's how your models get filtered. Our code above simply links to the index page with the id filter pre-set. You can add other filters, sort orders, or even scopes as well.

Easiest way for removing labels from Rails Admin forms?

Is there any simple way for removing the labels under forms for New resource in RailsAdmin?
I have "Optional. Length up to 255." under most of the fields although those are mandatory and it's confusing to end users.
I don't have any manually added models, I just use the default config:
config.actions do
dashboard
index
new
export
bulk_delete
show
edit
delete
show_in_app
end
Thanks in advance!
rails_admin looks at model validations to determine the help text for each field. Since your fields are being marked Optional, I'm guessing they're mandatory by some other means.
You can turn off the help messages using the help false statement. If you'd like to do it for all fields on the form, you can use the fields method:
config.model 'Bug' do
edit do
fields do
help false
end
end
end
RailsAdmin determines whether a field is optional via the presence validation in the model.
To remove the optional label, add this line into your model:
validates_presence_of :attribute1, :attribute2

django tables2 create extra column with links

I am trying to add a extra column to one of my tables, that adds url to another page.
My Table:
class ItemTable(tables.Table):
edit = tables.LinkColumn('item_edit', args=[A('pk')])
class Meta:
model = Item
fields = ('name', 'slot', 'klass', 'rarity', 'price')
my urls:
url(r'^admin/item/edit/(?P<item_id>\d+)/$', views.item_edit, name='item_edit')
Now with this, i get my table, but the last column (edit) only has dashes + the page crashes when i click the title.
i have been looking at http://django-tables2.readthedocs.org/en/latest/#django_tables2.columns.LinkColumn and im not sure where i go wrong
The problems you've encountered are caused by LinkColumn expecting to be bound to a specific attribute in your Item model, i.e. it is looking for an Item.edit attribute on your instances.
Since you don't actually have an Item.edit attribute, ordering over your edit column makes no sense, and you should mark it as non-orderable:
from django_tables2.utils import A
edit = tables.LinkColumn('item_edit', args=[A('pk')], orderable=False)
The text of the link itself would come from the value of the Item.edit attribute, which you don't have, so you will need to provide it yourself by adding a render_edit method to your table class:
def render_edit(self):
return 'Edit'
You can replace the 'Edit' string with whatever you want displayed in that column.
Update: As suggested by #SunnySydeUp, you also need to specify empty_values=() for the column, in order to get its value rendered:
edit = tables.LinkColumn('item_edit', args=[A('pk')], orderable=False, empty_values=())
References:
http://django-tables2.readthedocs.org/en/latest/pages/order-by-accessors.html#specifying-alternative-ordering-for-a-column
http://django-tables2.readthedocs.org/en/latest/pages/custom-rendering.html#table-render-foo-methods
Disclaimer: This answer is based on the django-tables2 documentation and source code, and hasn't been tested on an actual Django application.
To have the link properly formatted and with a link text of your choice, you can do the following in the table class:
def render_edit_link(self,record):
return mark_safe('<a href='+reverse("edit", args=[record.pk])+'>Edit</a>')
Where 'edit' is the name of the url.
I create clickable links in extra columns with
edit = tables.LinkColumn('item_edit', text='Edit', args=[A('pk')], \
orderable=False, empty_values=())
It's not necessary to override the render method; the 'text' parameter changes the text of the link from 'none' to 'Edit', for example.

Multi select file upload field with nested attributes in Rails

Currently I have a Note model which accepts nested attributes for an Attachments model, which uses Carrierwave. When adding a Note, I have a nested form to allow attaching file to the new Note:
Nested form field:
<%= f.file_field :image, multiple: true, name: "attachment[file]" %>
I am using the Cocoon gem to add the nested field. While I can easily let them add multiple file upload fields with Cocoon, and add multiple attachments that way, I only want to load one file upload field, and let them use multi select to select multiple images.
When I do this, the file upload field says '2 Images' next to it. However, upon form submission, only one file is listed under 'attachments_attributes'. I need all of the attachments to submit at once as the Note has not yet been saved.
What is the proper way to accomplish this? I am aware of the Railscast on this topic however it did not seem to address my particular scenario.
Any help is appreciated.
Just append [] to your params
<%= f.file_field :image, multiple: true, name: "attachment[file][]" %>

rails3-jquery-autocomplete foreign key

Hi I need help with setting up the rails3-jquery-autocomplete gem with a manually assigned foreign key in my database.
Here is what my models look like
class User < ActiveRecord::Base
has_many :reservations, :foreign_key => 'reserver_id'
attr_accessible :login, :first_name, :last_name
end
class Reservation < ActiveRecord::Base
belongs_to :reserver, :class_name => 'User'
attr_accessible :reserver, :reserver_id, :reserver_login
end
The users table in my database has all of the columns
The Reservations Controller has
autocomplete :users, :login
Routes.rb has
resources :reservations do
get :autocomplete_users_login, :on => :collection
end
And in the reservations view I have this
<%= f.autocomplete_field :reserver_id, autocomplete_users_login_reservations_path %>
Now when I try and test it I see that the calls are being made in my javascript console but I get the error 500. For example I tried searching for xno which is in a login column of my users database.
GET http://0.0.0.0:3000/reservations/autocomplete_users_login?term=xno 500 (Internal Server Error) jquery.js:8241
jQuery.ajaxTransport.send jquery.js:8241
jQuery.extend.ajax jquery.js:7720
jQuery.each.jQuery.(anonymous function) jquery.js:7246
jQuery.extend.getJSON jquery.js:7263
a.railsAutocomplete.fn.extend.init.a.autocomplete.source autocomplete-rails.js:17
$.widget._search jquery-ui.js:6547
$.widget.search jquery-ui.js:6540
(anonymous function) jquery-ui.js:6335
Does this have anything to do with the fact that I am using foreign key in my database? If yes how should I structure my routing. I was following the gem documentation and tried to set it up so that I can look up the users table and list people by the login column of the users table, but so that the id gets returned and stored as :reserver_id once the user is selected. Previous code that worked with select field was
<%= f.select :reserver_id, User.select_options, :prompt => true %>
where select_options method creates an array of login strings and id pairs in individual arrays.
I know that the solution is probably really easy, thank you for your help...
My problem was silly but it turned out it had nothing to do with the fact that the category table used a custom foreign key. My first problem was using plural version of the table name rather than the singular. Once I changed all references to users to user I noticed that javascript console wasn't throwing any exceptions at me so I figured that I had either broken everything and the call wasn't being made or it was going through and it wasn't rendering properly.
The presence of proper MYSQL calls in the console confirmed my suspicion. That's when I saw that the list was rendering way off to the right of my page probably due to strange styling happening somewhere else.
Bottom line is having a custom foreign key doesn't change usage of the gem at all. THe original table name should be used.