Skip to content

Instantly share code, notes, and snippets.

@tetsuyainfra
Last active March 5, 2019 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tetsuyainfra/91b20438fa28bfa7ab24510b5d2a59c0 to your computer and use it in GitHub Desktop.
Save tetsuyainfra/91b20438fa28bfa7ab24510b5d2a59c0 to your computer and use it in GitHub Desktop.
ActiveAdminでPOSTパラメータを加工してUpdateするメモ ref: https://qiita.com/tetsuyainfra/items/390652f179f329779f9d
ActiveAdmin.register Post do
permit_params :uid, :name, :meta, :related_ids
form do |f|
f.semantic_errors # shows errors on :base
f.inputs do
f.input :uid
f.input :name
f.input :meta, as: :text # json,jsonb型をtextとして扱う
f.input :related_uids, as: :string # arrayをstringとして扱う
end
f.actions
end # form
controller do
# 既メソッドを利用してパラメータを加工する
# https://github.com/activeadmin/inherited_resources/blob/46493ce10fc7d9f0bcd753f5d298c67ca83cf9b0/lib/inherited_resources/base_helpers.rb#L316
# https://github.com/activeadmin/inherited_resources/blob/46493ce10fc7d9f0bcd753f5d298c67ca83cf9b0/lib/inherited_resources/base_helpers.rb#L356
def nomralized_params
resource_params.map{ |p|
p.merge(
related_uids: p[:related_uids].split.map{&:to_i}
)
}
end
# メソッドをオーバーライドする
# https://github.com/activeadmin/inherited_resources/blob/46493ce10fc7d9f0bcd753f5d298c67ca83cf9b0/lib/inherited_resources/actions.rb#L42
def update(options={}, &block)
object = resource
if update_resource(object, nomralized_params)
options[:location] ||= smart_resource_url
end
respond_with_dual_blocks(object, options, &block)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment