{"id":150,"date":"2015-02-18T17:11:19","date_gmt":"2015-02-18T08:11:19","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2015\/02\/18\/activeadmin"},"modified":"2022-10-26T09:33:55","modified_gmt":"2022-10-26T00:33:55","slug":"activeadmin","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2015\/02\/18\/activeadmin\/","title":{"rendered":"ActiveAdmin\u3067\u3061\u3087\u3063\u3068\u4f7f\u3044\u52dd\u624b\u306e\u826f\u3044\u7ba1\u7406\u753b\u9762\u3092\u4f5c\u308bTips"},"content":{"rendered":"
\u6700\u8fd1\u306fJava\u3067\u30d0\u30c3\u30c1\u51e6\u7406\u3092\u66f8\u3044\u3066\u3044\u308b\u3053\u3068\u304c\u591a\u3044\u4e0b\u689d\u3067\u3059\u3002\u81ea\u5206\u306e\u4e2d\u3067\u30a8\u30f3\u30bf\u30fc\u30d7\u30e9\u30a4\u30baJava\u71b1\u304c\u4e0a\u304c\u3063\u3066\u304d\u307e\u3057\u305f\u3002\u306a\u306e\u3067\u3001Java\u306e\u8a71\u984c\u3067\u3082\u3044\u3044\u3093\u3067\u3059\u3051\u3069\u3001\u3067\u3082\u50d5\u306fRails\u3002<\/p>\n
Ruby on Rails\u3067\u7c21\u5358\u306b\u7ba1\u7406\u753b\u9762\u3092\u4f5c\u308a\u305f\u3044\u3068\u304d\u306b\u4f7f\u3048\u308bgem\u3067\u3042\u308bActiveAdmin\u3002\u7ba1\u7406\u753b\u9762\u306f\u4e00\u822c\u7684\u306b\u3042\u307e\u308a\u898b\u305f\u76ee\u306e\u7d20\u6575\u3055\u306f\u8981\u6c42\u3055\u308c\u306a\u3044\u3053\u3068\u304c\u591a\u304f\u3001ActiveAdmin\u3067\u3042\u308b\u7a0b\u5ea6\u306e\u30ec\u30d9\u30eb\u306e\u7ba1\u7406\u753b\u9762\u304c\u7c21\u5358\u306b\u4f5c\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002\u305f\u3060\u3001\u672c\u5f53\u306b\u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u307e\u307e\u3060\u3068\u3068\u3066\u3082\u4f7f\u3044\u306b\u304f\u3044\u7ba1\u7406\u753b\u9762\u306b\u306a\u3063\u305f\u308a\u3059\u308b\u3002\u554f\u984c\u3068\u3057\u3066\u306f\u4f8b\u3048\u3070\u3001<\/p>\n
\u306a\u3069\u306a\u3069\u3002\u4eca\u56de\u306f\u3001\u7c21\u5358\u306a\u30b5\u30f3\u30d7\u30eb\u30a2\u30d7\u30ea\u3092\u4f5c\u308a\u306a\u304c\u3089\u3053\u308c\u3089\u3092\u6539\u5584\u3057\u3066\u3044\u3063\u3066\u307f\u308b\u3002<\/p>\n
\u7c21\u5358\u306a\u4f8b\u3068\u3057\u3066\u3001\u66f8\u7c4d\uff08Book\uff09\u3068\u8457\u8005\uff08Author\uff09\u3092\u7ba1\u7406\u3059\u308b\u30a2\u30d7\u30ea\u3092\u4f5c\u3063\u3066\u3044\u304d\u306a\u304c\u3089\u898b\u3066\u3044\u304f\u3053\u3068\u306b\u3059\u308b\u3002\u30e2\u30c7\u30eb\u306f\u4ee5\u4e0b\u3002Book\u3068Author\u306f\u304a\u4e92\u3044\u306bhas_many\u306e\u95a2\u4fc2\u306b\u306a\u3063\u3066\u304a\u308a\u3001\u4e2d\u9593\u30e2\u30c7\u30eb\u304c\u5b58\u5728\u3059\u308b\u4f8b\u3068\u3059\u308b\u3002<\/p>\n
<\/p>\n
\u30e2\u30c7\u30eb\u3084\u3001\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u30d5\u30a1\u30a4\u30eb\u4f5c\u6210\u306e\u624b\u9806\u306f\u3053\u3093\u306a\u611f\u3058\u3067\u3002<\/p>\n
$ rails g model Author name:string description:text\n$ rails g model Book title:string isbn:string published_date:date\n$ rails g model AuthorBook author_id:integer book_id:integer\n$ rake db:migrate<\/code><\/pre>\n\u305d\u308c\u305e\u308c\u306e\u30e2\u30c7\u30eb\u306bhas_many\u3068belongs_to\u306a\u3069\u3092\u8ffd\u52a0\u3002\u4f8b\u3048\u3070app\/model\/book.rb\u306f\u3053\u3093\u306a\u611f\u3058\u3002\uff08\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u306a\u3093\u304b\u306f\u30b5\u30dc\u3063\u3066\u307e\u3059\u3002\uff09<\/p>\n
class Book < ActiveRecord::Base\n has_many :author_books, dependent: :destroy\n has_many :authors, through: :author_books\n accepts_nested_attributes_for :author_books, allow_destroy: true\nend<\/code><\/pre>\n\u3055\u3063\u305d\u304fActiveAdmin\u5c0e\u5165\u3002Gemfile\u306b\u4ee5\u4e0b\u3092\u8ffd\u52a0\u3057\u3001<\/p>\n
gem 'activeadmin', github: 'activeadmin'\ngem 'inherited_resources', github: 'josevalim\/inherited_resources', branch: 'rails-4-2'<\/code><\/pre>\nbundle install\u3057\u305f\u5f8c\u3001<\/p>\n
$ rails g active_admin:install --skip-users<\/code><\/pre>\n\u3068\u308a\u3042\u3048\u305a\u4eca\u56de\u306f\u7c21\u5358\u306e\u305f\u3081\u3001'--skip-users'\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u8ffd\u52a0\u3057\u305f\u305f\u3081\u3001\u30e6\u30fc\u30b6\u306a\u3057\u3067\u4f7f\u3048\u308b\u3002<\/p>\n
http:\/\/localhost:3000\/admin<\/a><\/p>\n\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u65e9\u901f\u7ba1\u7406\u753b\u9762\u304c\u3067\u304d\u3066\u3044\u308b\u3002
\n\u203bconfig\/initializers\/adtive_admin.rb\u3067
\nconfig.current_user_method = :current_admin_user
\n\u3092\u30b3\u30e1\u30f3\u30c8\u30a2\u30a6\u30c8\u3057\u3066\u304a\u304f\u3002<\/p>\n
<\/p>\n
\u3053\u3053\u3067\u3001\u3068\u308a\u3042\u3048\u305a\u5168\u3066\u306e\u30e2\u30c7\u30eb\u3092ActiveAdmin\u306e\u7ba1\u7406\u5bfe\u8c61\u306b\u52a0\u3048\u3066\u307f\u308b\u3002<\/p>\n
$ rails g active_admin:resource author\n$ rails g active_admin:resource book\n$ rails g active_admin:resource author_book<\/code><\/pre>\n\u7d50\u679c\u3001app\/admin\/author.rb, app\/admin\/book.rb, app\/admin\/author_book.rb\u306e\u30d5\u30a1\u30a4\u30eb\u304c\u8ffd\u52a0\u3055\u308c\u308b\u3002
\n\u3042\u3068\u306f\u3001Strong Parameters\u5bfe\u5fdc\u3067\u3001permit_params\u3092\u8a2d\u5b9a\u3057\u3066\u304a\u304f\u3002\uff08\u305f\u3076\u3093\u5404\u30d5\u30a1\u30a4\u30eb\u306b\u30b5\u30f3\u30d7\u30eb\u304c\u66f8\u304b\u308c\u3066\u3044\u308b\u3068\u601d\u3046\u3002\uff09<\/p>\n
\u3053\u308c\u3067\u3072\u3068\u901a\u308a\u306eCRUD\u64cd\u4f5c\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u3063\u305f\u3002\u3055\u3059\u304cActiveAdmin\u3002\u65e9\u3044\u3002\u4f8b\u3048\u3070\u66f8\u7c4d\u306e\u767b\u9332\u753b\u9762\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308b\u3002<\/p>\n
<\/p>\n
\u591a\u5bfe\u591a\u306e\u4e2d\u9593\u30e2\u30c7\u30eb\u306e\u30c7\u30fc\u30bf\u767b\u9332<\/h2>\n
\u3057\u304b\u3057\u3001\u554f\u7b54\u7121\u7528\u3067\u30e2\u30c7\u30eb\u3092ActiveAdmin\u3067\u306e\u7ba1\u7406\u5bfe\u8c61\u306b\u3059\u308b\u3068\u3001\u4e0d\u4fbf\u306a\u7ba1\u7406\u30d5\u30ed\u30fc\u3068\u306a\u3063\u3066\u3057\u307e\u3046\u5834\u5408\u304c\u3042\u308b\u3002\u591a\u5bfe\u591a\u306e\u4e2d\u9593\u30e2\u30c7\u30eb\u306f\u305d\u306e\u4ee3\u8868\u4f8b\u3067\u3042\u308b\u3002
\nActiveAdmin\u306f\u30e2\u30c7\u30eb\u3092\u3082\u3068\u306b\u3001\u305d\u306e\u30e1\u30f3\u30d0\u3092\u767b\u9332\u3001\u7de8\u96c6\u3001\u524a\u9664\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\u3060\u3051\u306a\u306e\u3067\u3001\u591a\u5bfe\u591a\u306e\u4e2d\u9593\u30e2\u30c7\u30eb\u3092ActiveAdmin\u3067\u306e\u7ba1\u7406\u5bfe\u8c61\u306b\u3059\u308b\u3068\u3001ID\u30922\u3064\u767b\u9332\u3059\u308bUI\u3068\u306a\u308b\u3002
\n\u4f8b\u3048\u3070\u4eca\u56de\u306e\u30b5\u30f3\u30d7\u30eb\u3067\u306f\u3001\u66f8\u7c4d\u3068\u8457\u8005\u3092\u7d50\u3073\u3064\u3051\u308b\u767b\u9332\u753b\u9762\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308b\u3002<\/p>\n
<\/p>\n
\u3046\u30fc\u3080\u3002\u65e5\u672c\u8a9e\u5316\u3055\u308c\u3066\u306a\u3044\u3068\u3044\u3046\u3068\u3053\u308d\u306f\u304a\u3044\u3066\u304a\u3044\u3066\u3001\u3053\u308c\u3060\u3068\u3001\u66f8\u7c4d\u3068\u8457\u8005\u3092\u7d10\u4ed8\u3051\u308b\u306e\u306b\u3001\u307e\u305a\u66f8\u7c4dID\u3068\u8457\u8005ID\u3092\u8abf\u3079\u3066\u304b\u3089\u3001\u3053\u306e\u753b\u9762\u3067ID\u3092\u5165\u308c\u308b\u3068\u3044\u3046\u3001\u304a\u305d\u3089\u304f\u975e\u5e38\u306b\u9762\u5012\u306a\u624b\u9806\u3068\u306a\u308b\u3002
\n\u3053\u3053\u306f\u3001\u66f8\u7c4d\u306e\u767b\u9332\u6642\u306b\u3067\u3082\u3001\u305d\u306e\u8457\u8005\u3092\u8907\u6570\u767b\u9332\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u305f\u3044\u3068\u3053\u308d\u3067\u3042\u308b\u3002
\n\u305d\u3053\u3067\u3001\u66f8\u7c4d\u306e\u767b\u9332\u753b\u9762\u3092\u5909\u66f4\u3057\u3066\u307f\u308b\u3002<\/p>\n
app\/admin\/book.rb
\n\u3067\u30d5\u30a9\u30fc\u30e0\u3092\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\u3059\u308b\u3002
\n\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u30d5\u30a9\u30fc\u30e0\u306e\u30ab\u30b9\u30bf\u30e0\u8a2d\u5b9a\u3092\u8ffd\u52a0\u3002\u3042\u3068\u306f\u3001permit_params\u3082\u8ffd\u52a0\u3002<\/p>\n
ActiveAdmin.register Book do\n permit_params :title, :isbn, :published_date, author_books_attributes: [:author_id, :_destroy, :id]\n\n form do |f|\n f.inputs '\u66f8\u7c4d\u767b\u9332' do\n f.input :title\n f.input :isbn\n f.input :published_date\n f.has_many :author_books, allow_destroy: true, heading: false,\n new_record: true do |ab|\n ab.input :author_id,\n label: '\u8457\u8005',\n as: :select,\n collection: Author.all.map { |a| [a.name, a.id] }\n end\n f.actions\n end\n end\nend<\/code><\/pre>\n\u3059\u308b\u3068\u3001\u3053\u3093\u306a\u611f\u3058\u3067\u30d5\u30a9\u30fc\u30e0\u304c\u3067\u304d\u3001\u66f8\u7c4d\u306e\u767b\u9332\u3001\u7de8\u96c6\u6642\u306b\u3001\u7d10\u4ed8\u3051\u308b\u8457\u8005\u3092\u8457\u8005\u540d\u3067\u767b\u9332\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u3001\u3061\u3087\u3063\u3068\u4fbf\u5229\u306b\u306a\u3063\u305f\u3002<\/p>\n
<\/p>\n
\u305f\u3060\u3001\u3053\u308c\u3060\u3051\u3060\u3068\u767b\u9332\u5b8c\u4e86\u753b\u9762\u306b\u306f\u8457\u8005\u540d\u304c\u51fa\u3066\u3053\u306a\u3044\u3002<\/p>\n
<\/p>\n
\u3064\u3044\u3067\u306b\u3001\u3053\u306e\u753b\u9762\u306bcreated_at\u3068update_at\u30ab\u30e9\u30e0\u306e\u60c5\u5831\u304c\u51fa\u3066\u3044\u308b\u306e\u3082\u3061\u3087\u3063\u3068\u90aa\u9b54\u3067\u3042\u308b\u3002\u3068\u3044\u3046\u308f\u3051\u3067\u3001\u305d\u308c\u3089\u3092\u524a\u9664\u3057\u3064\u3064\u3001\u8457\u8005\u540d\u3092\u8ffd\u52a0\u3057\u3066\u307f\u308b\u3002<\/p>\n
\u518d\u3073app\/admin\/book.rb\u3067\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\u3002\u4eca\u5ea6\u306f\u4ee5\u4e0b\u3092\u8ffd\u52a0\u3002<\/p>\n
show do\n attributes_table do\n row '\u30bf\u30a4\u30c8\u30eb' do\n resource.title\n end\n row 'ISBN' do\n resource.isbn\n end\n row '\u51fa\u7248\u65e5' do\n resource.published_date\n end\n row '\u8457\u8005' do\n Author.where(id: AuthorBook.where(book_id: resource.id).all.pluck(:author_id)).all.pluck(:name).join(',')\n end\n end<\/code><\/pre>\n\u7d50\u679c\u3002<\/p>\n
<\/p>\n
\u3060\u3044\u3076\u30de\u30b7\u306b\u306a\u3063\u305f\u3002<\/p>\n
\u307e\u305f\u3001\u591a\u5bfe\u591a\u306e\u4e2d\u9593\u30e2\u30c7\u30eb\u4ee5\u5916\u3067\u3082\u3001\u30e2\u30c7\u30eb\u8a2d\u8a08\u306b\u3088\u3063\u3066\u306f\u305d\u306e\u307e\u307eActiveAdmin\u3067\u7ba1\u7406\u3059\u308b\u3068\u4f7f\u3044\u52dd\u624b\u304c\u60aa\u304f\u306a\u308b\u5834\u5408\u304c\u591a\u3005\u3042\u308b\u306e\u3067\u3001\u30e2\u30c7\u30eb\u8a2d\u8a08\u304c\u91cd\u8981\u306b\u306a\u308b\u306e\u3068\u540c\u6642\u306b\u3001\u3082\u3057\u65e2\u5b58\u30e2\u30c7\u30eb\u3092ActiveAdmin\u3067\u7ba1\u7406\u3057\u305f\u3044\u3068\u3044\u3046\u6642\u306b\u306f\u3069\u3046\u3044\u3046UI\u306b\u306a\u308b\u306e\u304b\u3088\u304f\u8003\u3048\u305f\u307b\u3046\u304c\u3088\u3044\u3068\u601d\u308f\u308c\u308b\u3002\u307e\u3042\u3001ActiveAdmin\u306b\u5408\u308f\u305b\u308b\u3068\u3044\u3046\u306e\u306f\u672c\u672b\u8ee2\u5012\u3060\u3051\u308c\u3069\u3082\u3002<\/p>\n
\u65e5\u672c\u8a9e\u5316<\/h2>\n
\u4e0a\u8a18\u3067\u5c40\u6240\u7684\u306b\u65e5\u672c\u8a9e\u5316\u3057\u3066\u3044\u305f\u304c\u3001\u3061\u3083\u3093\u3068\u65e5\u672c\u8a9e\u3092\u3059\u308b\u306b\u306fI18n\u3092\u4f7f\u3046\u3002<\/p>\n
\u307e\u305a\u306fconfig\/locales\/ja.yml\u3092\u4f5c\u6210\u3002<\/p>\n