Setting locale for globalize in active admin - ruby-on-rails

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
...

Related

Test for ActiveAdmin create/edit form - "Add New" record button for associated object

I have an ActiveAdmin form to create a new ActiveRecord object, and the form is capable of also creating records for one of the "main" model's has_many associations, i.e.:
class Car < ApplicationModel
has_many :tires
end
class Tire < ApplicationModel
belongs_to :car
end
# ... in ActiveAdmin "cars.rb" file...
ActiveAdmin.register Car do
...
form do |f|
f.semantic_errors
f.inputs 'Car' do
f.input :color
f.input :doors, as: :number
f.inputs do
f.has_many :tires, allow_destroy: true, new_record: true do |t|
t.input :type
t.input :size
end
end
end
f.actions
end
end
There is also some controller logic not listed here that properly creates, edits, or destroys these records, including Cars and Tires. When the form renders (let's say I'm creating a new Car), there is a button at the bottom that says "Add New Tire". Clicking it renders a sub-form on the page with two fields: "Type" and "Size". I've set up the controllers to save both the Car and its associated Tires when I submit the form. That all works.
I'm struggling with an automated test to ensure that clicking the "Add New Tire" button causes the sub-form to render. I've added a breakpoint after clicking the button and inspected page.body to see if the sub-form exists, but it doesn't seem to be there - I only see what looks like a template of the sub-form in the button's (really an a element) code. This is the part of my test code that fails:
click_link 'New Car'
fill_in('Color', with: 'Blue')
fill_in('Doors', with: 2)
click_link('Add New Tire') # Succeeds
fill_in('Type', with: 'All-Terrain') # Fails, can't find element
I've tried other ways to match on the first sub-form field, like xpath and css selectors, but it just can't find the field, and like I mentioned above, I don't see an instance of the sub-form being rendered if I inspect the page with page.body. I'm stuck - can anyone help me figure out what's going on here?
I got a tip that adding js: true to the test definition could help, and as it turns out, it did:
RSpec.describe Car, js: true do
...
end

ActiveAdmin: form input without corresponding table attribute

How can I set a textfield in an ActiveAdmin form, who do not correspond to a table attribute ?
I need it to build an autocomplete behavior, to populate a list of checkboxes.
In case you want the value submitted back to your model, you can create a virtual attribute by adding attr_accessible, or explicitly define them in the model, as suggested in this answer:
def my_virtual_attr= (attributes)
#this will be evaluated when you save the form
end
def my_virtual_attr
# the return of this method will be the default value of the field
end
and you will need to add it to permit_params in the ActiveModel resource file.
In case you don't need the value submitted to the backend (needed for front-end processing for example), you can actually add any custom HTML to ActiveAdmin form, and this is an example code it:
ActiveAdmin.register MyModel do
form do |f|
f.semantic_errors # shows errors on :base
f.inputs "My Custom HTML" do
f.li "<label class='label'>Label Name</label><a class='js-anchor' href='#{link}'>Link Text</a><span></span>".html_safe
f.li "<label class='label'>Label 2 Name</label><input id='auto_complete_input'/>".html_safe
end
f.inputs "Default Form Attributes" do
f.inputs # builds an input field for every attribute
end
f.actions # adds the 'Submit' and 'Cancel' buttons
end
end
You can try to remove model prefix from the params name
ActiveAdmin.register MyModel do
form do |f|
f.input :custom, input_html: { name: 'custom' } # instead of 'my_model[custom]'
end
end

Rails active admin free search on select element

I am using active admin gem.
i have questions table that have relationship with user_id, when i want to add new questions the select element is showing with tons of users, there is a way to make this select element along with search input for user name?
ActiveAdmin.register Question do
permit_params :difficulty, :title, :user
form do |f|
actions
inputs 'Question Details' do
input :user, :as => :select, :collection => User.all
input :difficulty, as: :select, collection: [1,2,3,4]
input :title
end
actions
end
end
You can use a javascript plugin, I'd thoroughly recommend chosen
It will dynamically turn a regular form select into a searchable select with a simple Javascript call.
You can checkout some demos here:
http://harvesthq.github.io/chosen/
You can integrate it into activeadmin by adding the chosen-rails gem to your Gemfile
gem 'chosen-rails'
Then add the javascript to your app/assets/javascripts/active_admin.js.coffee
#= require chosen-jquery
Add the css to your app/assets/stylesheets/active_admin.scss
#import "chosen"
You can add it to all your select inputs by simply adding the following to app/assets/javascripts/active_admin.js.coffee
$ ->
$("#active_admin_content select").chosen()
You could also limit it to certain select inputs by adding a class to active admin inputs and only targeting those selects with a specific class like chosen-select

Pass a parameter to the new action in Active Admin

I have two related models, Bunny has_many BunnyData (which belongs_to Bunny). From the show page of a particular Bunny (in Active Admin), I want to create a link to make a related BunnyData. I've tried a few different ways, with no success, and am currently trying this:
sidebar :data, :only => :show do
link_to 'New Data', new_admin_bunny_datum(:bunny_id => bunny.id)
end
The link being generated ends up as something like:
.../admin/bunny_data/new?bunny_id=5
But when you go to that page, the dropdown for Bunny is set to the blank default as opposed to showing the name of Bunny with ID 5.
Thanks in advance.
Rails namespaces form fields to the data model, in this case BunnyData. For the form to be pre-filled, any fields provided must also include the namespace. As an example:
ActiveAdmin.register Post do
form do |f|
f.inputs "Post Details" do
f.input :user
f.input :title
f.input :content
end
f.actions
end
end
The fields can be pre-filled by passing a hash to the path helper.
link_to 'New Post', new_admin_post_path(:post => { :user_id => user.id })
Which would generate the following path and set the form field.
/admin/posts/new?post[user_id]=5
In the case of BunnyData, it might be slightly different due to the singular and plural forms of datum. But that can be verified by inspecting the generated HTML to find the name attribute of the inputs.

Rails 4 + ActiveAdmin: Attribute limited to a few values -- customizing ActiveAdmin form based on this?

So I have a CareerEntry model that has a fullintern attribute, which is a string that is supposed to specify whether the entry represents an internship or a full-time position. I limit the values that can appear in this attribute as follows:
validates_inclusion_of :fullintern, :in => ["Internship", "Full-time"]
However, in ActiveAdmin, the part in the edit form that deals with the fullintern attribute still has a text field. How do I make it a dropdown box where the admin can select either "Internship" or "Full-time"?
Thanks.
You can use Formtastic's input helpers to use a select input:
form do |f|
f.inputs "Details" do
f.input :fullintern, as: :select, collection: ["Internship", "Full-time"]
end
f.actions
end
See Formtastic's usage section for the full set of native capabilities.

Resources