Rails Kitchen

It's a place to write on stuff I learned recently.

In Page Editing in 'Active Admin' - Using Gem 'Best in Place'

| Comments

Best in place is a j Query based Ajax plug in that can help us to add in place editing to our application that takes profit of Restful server-side controllers to allow users to edit stuff with no need of forms. Usage of this gem in rails application is well documented in github page. You can checkout their live demo here. In this post I concentrating on how we can use best in place gem with is active admin pages.
To add Best in Place to our app we first need to add its gem to our application’s Gemfile and run bundle.
Gemfile
1
gem 'best_in_place', github: 'bernat/best_in_place'
Require best in place in active_admin.js.coffee and initialise it
1
2
3
4
5
#= require best_in_place
#= require best_in_place.purr

$(document).ready ->
  jQuery(".best_in_place").best_in_place()
To make text field editable in active admin show page
1
2
3
row :name do |project|
 best_in_place project, :name, :type => :input,:path =>[:admin,project]
end
To make select field editable
1
2
3
row :status do |project|
  best_in_place project, :status, :type => :select,:path =>[:admin,project],:collection => Project.statuses.map{|status| [status,status]}
end
Textarea can be make editable using following code
1
2
3
row :status_description  do |project|
  best_in_place project, :status_description, :type => :textarea,:path =>[:admin,project]
end
For Editable Datepicker with formated output use display_with
1
2
3
row "Planned Start Date" do |project|
  best_in_place project, :planned_start_date , :type => :date ,:path =>[:admin,project],  :display_with => lambda { |v| v.try(:strftime,'%b %d, %Y') }
end

Comments