Easiest way for removing labels from Rails Admin forms? - ruby-on-rails-4

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

Related

Active Admin Associations

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.

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.

In Django, how to validate a (multiple) cholice field when the choices are added by Javascript?

I have a ChoiceField where the choices are not known at the time the HTML is rendered; they are defined dynamically by the user and added to the form through Javascript, but the validation naturally fails since the selected option is not in the choices attribute (which is just an empty list). I have tried setting the field to a CharField, but then the validator just gets a string value that needs to be converted into a list before it van be used. Ah, and I'd like to avoid subclassing the field class as it's just for one occasion.
I hope this is clear. Any ideas?
Don't subclass the field class but override the clean_<yourfield> method in your Form class. See the docs here.

Customising specific fields in Django Admin change form

I have a couple of fields in my model that to which I wish to add a link that will allow the user to search for names/files (external from the application's database).
So what I would like is:
Field name: [text box] - LINK
Is there a straightforward django way of achieving this?
Cheers.
You need to change the widget that the form field uses to display the models information. You basically add some html after the input to link to where you want.
Here's some code I put together to create a widget that displays how many characters are left for a CharacterField so it's similar to what you are looking to do:
https://github.com/pastylegs/django-widget-charsleft/blob/master/widget_charsleft/widgets.py

How to display more things in admin.StackedInline

I have Article model and a Comment model. Comment is created in admin.py as admin.StackedInline, and it has several fields, notably content and lastUpdate. For lastUpdate, i have specified as follows: lastUpdate = models.DateTimeField('last update', auto_now=True). Understandably, lastUpdate is not displayed when i try to add new comment (or edit old ones). However, i would like it to display for older comments if possible, as a read only thing. Is there anyway of accomplishing that?
Thanks a lot!
Jason
I guess if this http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.readonly_fields doesn't do what you want it to do, have a look at this: In a Django form, how do I make a field readonly (or disabled) so that it cannot be edited?