LoginSignup
1
1

More than 5 years have passed since last update.

Active Admin で Helper モジュールを生やす

Last updated at Posted at 2015-04-15

Active Admin を利用するときに、Helper モジュールを生やしたくなることがあるかと思います。
今回はその方法を簡単にまとめておきます。

1. initialize ファイルで include

active admin の initialize ファイルで ApplicationHelper を include します。

# in config/initializers/active_admin.rb
ActiveAdmin.setup do |config|
    ....
end

module ActiveAdmin::ViewHelpers
  include ApplicationHelper
end

2. Helper モジュールの定義

app/helpers/active_admin/foo_helper.rb などを作成し、下記のように定義する。

# app/helpers/active_admin/foo_helper.rb
module ActiveAdmin::FooHelper
  def foo
    # do something
  end
end

そうすると、helper メソッドを呼び出すことができます。

# app/admin/products.rb
ActiveAdmin.register Product do
  #...
  show do |product|
    row "Test Helpers" do |product|
      # `app/helpers/*_helper.rb` に定義されたすべてのメソッドを参照できる
      foo
    end
  end
  #...
end

Ref.

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1