How can I customise model name in ActiveAdmin filter here?
This way doesn't work:
activerecord:
models:
category:
one: "категория"
other: 'категории'
filter :title, :label => "Change me"
filter :description, :label => "Change Me"
filter :user, :label => "Change Me"
You could also use I18n.t if you wanted to
filter :title, :label => (I18n.t "some.key.here")
ActiveAdmin uses metasearh ,so next rule works for me. (destination - parent model, rateplan - nested model)
Example:
en:
activerecord:
attributes:
destination:
rateplan: "RATEPLAN LOCALIZED NAME"
more info: https://github.com/ernie/meta_search#localization
UPD.
One more soulution is using fortastic rules for internalizaton
en:
formtastic:
labels:
rateplan: "RATEPLAN LOCAIZED"
more info: https://github.com/justinfrench/formtastic#internationalization-i18n
Active Admin uses formtastic to render filter as like the form fields. So you can customize you filters as you like as the formtastic'c customization options are really handy. For example. to change filters label: just use label: 'YOUR CUSTOM LABEL' option in filter method. You can use translations there as well
Related
I connected globalize to my project. The main aim was to translate some fields in my Book.rb Model via ActiveAdmin. I also added select input to Book page in Active Admin with set of locales (I18n). How can I pass locale when I submit form (updates Book), so that translation would be saved in that locale?
Globalize documentation doesn't have enough examples. They suggest we can use Book.attributes{title: "some title", locale: :en}. But I have no idea how to implement that for my case.
admin/Book.rb
...
permit_params :title, :pages
form do |f|
inputs "Book info" do
input :title
input :pages
input :set_locale as :select, collection => [:ru, :en]
end
actions
end
...
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.
How do you simply validate that a checkbox is checked in rails?
The checkbox is for a end user agreement. And it is located in a modal window.
Lets say i have the checkbox:
<%= check_box_tag '' %>
Where and how should i validate this?
I have seen most posts about checkbox validation in rails here, but none of them suit my needs.
Adding
validates :terms_of_service, :acceptance => true
to your model should do it. Look here for more details and options.
However, if accepting the terms is not part of a form for your model, you should use client-side validations, i.e. JavaScript, like this (in jQuery):
function validateCheckbox()
{
if( $('#checkbox').attr('checked')){
alert("you have to accept the terms first");
}
}
You can add a script file to your view like this:
<%= javascript_include_tag "my_javascipt_file" %>
and trigger the function on click:
<%= submit_tag "Submit", :onclick: "validateCheckbox();" %>
EDIT: you can assign an id to your checkbox like this: check_box_tag :checkbox. The HTML will look like this: <input id="checkbox" See these examples for more options.
I was able to skip the jQuery portion and get it validation to work with this questions help. My method is below, I'm on Rails 5.1.2 & Ruby 2.4.2.
Put this code in your slim, erb or haml; note syntax may differ slightly in each.
The line below was specifically for slim.
f.check_box :terms_of_service, required: true
I used a portion of kostja's code suggestion in the model.
validates :terms_of_service, :acceptance => true
Adding on to what has been said already, if you want to add a custom error message, you can add the following to your form:
f.input :terms_of_service, as: :boolean
and then add the following to your model:
validates :terms_of_service, acceptance: { message: "must be accepted"}
Error messages will start with the field name by default followed by your custom message (e.g. Terms of service [CUSTOM MESSAGE]). Something I also found useful was to include a link to the terms of service in the label so users can easily access it to see what they are agreeing to:
f.input :terms_of_service, as: :boolean, label: "I agree to the #{link_to "terms of service", [TERMS AND CONDITIONS PATH]}".html_safe
I need to pass in a variable to generate the label of a field with simple form. With normal translations you go about this in the following way: http://guides.rubyonrails.org/i18n.html#passing-variables-to-translations but for the life of me I am unable to get it to work with simple form.
I am attempting to pass it in using:
= f.input :name, :contact_type => f.object.contact_type.to_s
And in the simple_form.en.yml file:
en:
simple_form:
labels:
contacts:
name: "Name %{contact_type}"
This is outputting:
Name %{contact_type}"
Ignoring the variable all together. Is there a way to accomplish this?
Thanks,
Ryan Lundie
You need to explicitly add the label to the form input like this :
= f.input :name,
:contact_type => f.object.contact_type.to_s,
:label => t(:'simple_form.labels.contacts.name', :contact_type => "Whatever contact type")
I'm using the simple_form gem. I want to customize the error message displayed when a user fails validations. How can I accomplish this?
You can declare the content of the
error message in your model:
validates_length_of :name, :minimum => 5, :message => "blah blah blah"
You can set id or class for your
error tag:
<%= f.input :name, :error_html => { :id => "name_error"} %>
Then you can use CSS for the styling.
And you can use
<%= f.error :name, :id => "name_error" %>
and you'll get
<span class="error" id="name_error">is too short (minimum is 5 characters)</span>
I dont know if it is any different for simple_form gem.
For content of error messages to be changed, you can use the :message attribute in the model.
class User < ActiveRecord::Base
validates :email, {:presence => true, :message => "is not filled up."}
end
Now the validation message will be Email is not filled up. If you want the field name also to be changed(Email to E-mail address something like that ), the approach now is to define it in locales.rb file like this
# config/locales/en.yml
en:
activerecord:
attributes:
user:
email: "E-mail address"
See link for details on locales. Another approach is to define in the model, humanized attributes like this:
class User < ActiveRecord::Base
validates :email, {:presence => true, :message => "is not filled up."}
HUMANIZED_ATTRIBUTES = {
:email => "E-mail address",
...(other fields and their humanized names)
...
}
def self.human_attribute_name(attr, options={})
HUMANIZED_ATTRIBUTES[attr.to_sym] || super
end
end
For customizing style of validation message we will have to edit the style for
#errorExplanation and .fieldWithErrors,in the scaffold.css stylesheet.
You can easily change the default error message comes in the translation file, which is found in config/locales/simple_form.en.yml.
In the specific initializer, config/initializers/simple_form.rb you can overrule the default options how the html is generated.
Hope this helps.
For completeness, I would like to add that formtastic is an easier choice to start with, because it has a default layout. I like simple_form a lot, but it does not offer any formatting out of the box, but that is their intention. With Formtastic it is very hard (impossible) to change the generated html, and with simple_form can you can completely mold the generated html to your liking. This is especially useful if you have a designer, and the forms you generate have to generate the same html. So if you are getting started, formtastic will give you nicer-looking results quicker. Also note that it is quite easy to switch, because the syntax is almost identical.
There is another solution explained here that wasn't mentioned in the answers. You can directly override the error messages from the views, in the form itself. For example:
<%= f.input :last_name,
placeholder: 'last_name',
error: 'This is a custom error message',
required: true,
class: 'form-field',
autofocus: true,
input_html: { autocomplete: "last_name" } %>
It is however not advised as it is not DRY, you would need to override it in every field.