How to change Active Admin input button text - ruby-on-rails

I have a form in my ActiveAdmin model and I need to translate the content of the button that is generated to upload a file.
I have the following form:
form do |f|
f.inputs 'Details' do
f.input :orders_file, as: :file
end
actions
end
Which displays this:
I would like to translate or change that 'Choose file' and 'No file chosen' texts.
I've tried with
input_html: { title: 'this', text: 'will', label: 'work', value: 'please' }
but no luck.
Thank you!

Changing the file upload field labels involves some special techniques invented by Michael McGrady, discussed in detail at https://www.quirksmode.org/dom/inputfile.html

You can not change the file field default labels on buttons and text, they are hard-coded in browsers. Each Browser has its own way to render these things.
Only way to change these labels is, build your own custom file field control.

Related

text_field_tag with ActiveAdmin

I've a hard time with ActiveAdmin and their DSL. I'm using it to build my admin and at some point in a form I need to have a text_field_tag; I mean some fields which aren't related to the model I'm manipulating in the form which will be sent through with the model related data.
A custom text field basically. Nothing too crazy.
So I've built this
panel 'Send payment authorization' do
active_admin_form_for EventPaymentAuthorization.new, url: { action: :send_event_payment_authorization } do |f|
f.inputs do
f.input :body, as: :text
f.text_field_tag :line_items_label
f.text_field_tag :line_items_amount
f.input :fees_in_cents, as: :select, collection: [:free, :automatic], prompt: true, selected: :automatic
end
f.actions do
f.action :submit, label: 'Create payment authorization'
end
end
end
The f.text_field_tag get simply ignored by ActiveAdmin. Why is that? It doesn't raise any error, but it doesn't show either.
The reason I need custom unrelated inputs is because line_items in my example is a JSONB with values such as [{amount: 0.0, label: 'Hello'}] and I don't believe it can be processed through Formtastic or ActiveAdmin natively. It's also always good to be able to create custom inputs when sending data.
Working with JSON in ActiveAdmin is a bit tricky, it largely depends on your needs. The quickest way to get going is to use the activeadmin_json_editor gem. I also wrote a blog entry on working with JSON in ActiveAdmin with more detail and another approach, which may better suit your needs, as it appears you are not just working with arbitrary data into your JSON field.

Is there any way to use formtastic in activeadmin batch action form?

According to activeamdin document, we can do:
batch_action :flag, form: {
type: %w[Offensive Spam Other],
reason: :text,
notes: :textarea,
hide: :checkbox,
date: :datepicker
} do |ids, inputs|
# inputs is a hash of all the form fields you requested
redirect_to collection_path, notice: [ids, inputs].to_s
end
However, the above form is not formtastic and doesn't support advanced table configuration(set the size for the form window). Is there any way that I can change it to formtastic format like:
form do |f|
f.semantic_errors # shows errors on :base
f.inputs # builds an input field for every attribute
f.actions # adds the 'Submit' and 'Cancel' buttons
end
Probably not. The form is built in batch_action_form.rb but rendered dynamically by the front end in modal_dialog.js.coffee, which is currently using jQuery. It is possible to get creative rewriting batch action forms but I can't recommend it. If your batch actions are complex try seeing if Custom Pages can meet your needs.

How to override text field to textarea in rails_admin

First time I try to customize rails_admin and by default I am getting text field which I want to convert in textarea. In model I have given datatype as string So is that possible to display textarea?
config/initializer/rails_admin.rb
config.model Product do
list do
exclude_fields :id, :created_at, :updated_at
end
create do
......
configure :description do
partial 'my_partial_file' # to override field I have created partial file
end
.....
end
end
views/rails_admin/main/_my_partial_file.html.haml
= form.send field.view_helper, field.method_name, field.html_attributes.reverse_merge({ value: field.form_value, checked: field.form_value.in?([true, '1']), class: 'form-control', required: field.required})
I tried html_attributes rows: 50, cols: 60 also tried to apply custom_css but doesn't help. Please guide me where I do mistake? And if possible please make me understand syntax of this _my_partial_file
Edit:
If I do something like this then I can get textarea
field :description, :text do # use second parameter to set field type
required true
#partial 'my_partial_file'
end
But if I render partial then again text_field shown. :( I want text_area + partial file should also rendered as it contain other code to process.
I have solved this issue by my own. I thought to delete this question but then realized if in future anyone faced same issue then my solution can be helped.
What I had changed in _my_partial_file is:
= form.text_area field.method_name, field.html_attributes.reverse_merge({ value: field.form_value, checked: field.form_value.in?([true, '1']), class: 'form-control', required: field.required})
........ # other piece of code
and in config/initializer/rails_admin.rb I have keep the code as it is:
create do
......
configure :description do
partial 'my_partial_file'
end
.....
end
I got the reference from here: https://www.omniref.com/ruby/gems/obitum-rails_admin/0.0.5/files/app/views/rails_admin/main/_form_text.html.haml#line=5 (Wayback link)
and
http://ruby-doc.org/gems/docs/r/rails_admin_settings-0.8.0/app/views/rails_admin/main/_setting_value_html_haml.html
(Note: link dead. This may be the same file from that older version: https://github.com/rails-admin/rails_admin_settings/blob/v0.8.0/app/views/rails_admin/main/_setting_value.html.haml )

How to create a custom method for the hint parameter of the input method in ActiveAdmin/Simple Form gems?

From this code:
ActiveAdmin.register News do
form do |f|
f.inputs do
f.input :url
f.input :site_name
f.input :site_url
f.input :image,
as: :file,
hint: if f.object.image_url
f.template.image_tag(f.object.image_url)
else
''
end
end
f.actions
end
end
I want to extract the if clause in a method, since I have this part repeating a lot. The question is - how exactly do I do that? To clarify:
Where do I define the method - in some helper, in a new file? How do I make sure ActiveAdmin has access to this file? Do I have to create a custom ActiveAdmin controller action?
Although something like some_method(f, field) (field is in my case image) is acceptable, I would like to have it in a more generic form, so that I repeat myself less, like f.hint with the hint method inferring the field name as the first parameter of f.input. Is this possible?
Any advice on this is appreciated.
Probably the easiest way would be to simply create your own formtastic input for images that either set the hint option by default, or add in extra HTML to show an image preview. Doing so might allow you to do something like this:
f.input :image, as: :image, preview: f.object.image_url

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.

Resources