I am using on Rails 4 and ActiveAdmin, a batch action collection form to capture input from the user as they perform a batch action ( in this case a batch mailer). Is there a way to customize the labels for the inputs?
Say for example:
batch_action :email, priority: 1 , form: {main_subject: :text,
message: :textarea
} do |ids, inputs|
batch_action_collection.find(ids).each do |user|
ContactBatchMailer.contact_batch_email(main_subject, message,...
Instead of having "main_subject", I would like to display a better formatted text, like "Main Subject", or even better, something more descriptive than the variable name by itself.
I dug in the documentation https://github.com/activeadmin/activeadmin/blob/master/docs/9-batch-actions.md#batch-action-forms but I was not able to.
Any suggestions will be much appreciated.
The form is rendered by modal_dialog.js.coffee, it should be possible to override and customize, for example see this declined pull request from #mmustala
You don't have to use a symbol for the form input name. A string will also work. So in your case you could do:
batch_action :email, priority: 1 , form: {'Main Subject': :text,
message: :textarea
} do |ids, inputs|
main_s = inputs['Main Subject']
...
end
Related
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.
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
I have a form. Many of the labels need to have a different name than they are given in the model.
attr_accessible :pin
Simpleform view:
= f.input :pin, :label => "Secret Code"
When validation fails, the error uses the model's name for the field ("Pin"), rather than the label I set in the view ("Secret Code"). The user will be confused on what field has the error.
Currently I have a helper that changes the label:
def fix_pin_errors(msg)
msg.gsub!('Pin', 'Secret Code')
end
If I am doing this for many fields, however, it becomes tedious and not DRY.
What is the best way to have errors match labels that differ from the names the model gives them?
I18n is your friend. Read this guide. It will explain how to globally convert these so they get picked up everywhere you need.
For your example, it might look something like this:
In en.yml:
en:
activerecord:
attributes:
your_model_name:
pin: 'Secret Code'
I am using simple_form and have a following sample tag:
<%= f.input :medical_conditions, :label=>false, :collection => medical_conditons, :as => :check_boxes%>
The collection holds about 100 checkboxes. However, when user only selects 1 or 2, everything is still getting saved to the database like this:
---
- ""
- ""
- ""
medical_conditions is a simple array in my application_helper
def medical_conditons
t = [
"Allergies/Hay Fever",
"Diabetes",
"Heart Surgery"]
return t
end
the medical_conditions field is a :string field.
What do I need to do so that only values that are selected are saved in comma separated manner.
It is not simple_form behavior. It is from Rails. See this: http://d.pr/6O2S
Try something like this in your controller (guessing at how you wrote your create/update methods)...
params[:medical_conditions].delete('') #this will remove the empty strings
#instance.update_attribute(:medical_conditions, params[:medical_conditions].join(','))
#or however you want to save the input, but the key is the .join(',') which will
#create a comma-separated string of only the selected values, which is exactly
#what you're looking for me thinks :-)
If that does the trick for you, I'd consider making a private helper method that formats the params for you so that you can use it in #create, #update, or wherever else you need it. This should keep things a little cleaner and more "rails way-ish" in your crud actions.
Say I have the following model:
class Information < ActiveRecord::Base
...
validates_length_of :name, :minimum=>3, :message=>"should be longer than 3 characters!"
...
What I want to have as an error is:
Information should be longer than 3 characters! (or similar)
and NOT "Information name should be longer than 3 characters!".
Two possible workarounds I've looked at:
human_attribute_name method (mentioned here): doesn't work with my Rails 2.3.2. :-(
directly do a information.errors.add "","..." if information.name.length < 3: however, this removes many useful properties triggered by the validated_length_of method like the special class-tags (for coloring the stuff red).
Any ideas? Thank you for your time.
I suppose that you display errors through full_messages method, which is meant for console, not for web application use. You should use error_message_on or error_messages_for helpers instead (see documentation for more info), which allow you to customize error messages.
Example:
<%= error_message_on "information", "name", :prepend_text => 'Information ' %>
don't use the rails helper to make the errors, normally i have inline errors so something like:
def inline_error_block(obj, meth, prepend="", append="", klass="error error-form", &block)
content = capture(&block)
obj = (obj.respond_to?(:errors) ? obj : instance_variable_get("##{obj}"))
if obj
errors = obj.errors.on(meth.to_s)
if errors
output = content_tag(:div, :class => klass) do
content_tag(:p, "#{prepend}#{errors.is_a?(Array) ? errors.first : errors}#{append}", :class => "error-msg clearfix") + content
end
return concat(output)
end
end
concat(content_tag(:div, content, :class => "no-error"))
end
tends to do the trick, but, it only shows one error per form field, am sure you could rearrange it to show them all, should you need to! (errors.first to errors.each).
To get the full name, just write the message with the field name as you want it displayed:
validates_length_of :name, :minimum=>3, :message=>"Information should be longer than 3 characters!"
You could always set your :message to an empty string in the model then set the :prepend_text in the view to whatever you like.