Ruby-on-rails Rails 3. 如何添加 ActiveAdmin 将使用的帮助程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8673112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:32:30  来源:igfitidea点击:

Rails 3. How to add a helper that ActiveAdmin will use?

ruby-on-railsrubyhelpersactiveadmin

提问by leonel

I'm creating a helper to be used by Formtastic but I get the undefined local variable or methoderror. I don't know where to put it so it can work.

我正在创建一个供 Formtastic 使用的帮助程序,但undefined local variable or method出现错误。我不知道把它放在哪里,所以它可以工作。

I already tried in the application_helper.rb and in app/helpers/active_admin/view_helpers.rb

我已经在 application_helper.rb 和 app/helpers/active_admin/view_helpers.rb 中尝试过

回答by Bishma Stornelli

You can define them in app/helpers/ as you tried but you need to include them trough the active admin's initializer like this:

您可以按照尝试在 app/helpers/ 中定义它们,但您需要通过活动管理员的初始值设定项包含它们,如下所示:

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

module ActiveAdmin::ViewHelpers
  include ApplicationHelper
end

回答by alony

You need to put your helper functions in app/helpers/active_admin/views_helper.rbfile Example:

您需要将辅助函数放在app/helpers/active_admin/views_helper.rb文件示例中:

module ActiveAdmin::ViewsHelper #camelized file name
  def my_helper 
       # do something 
  end 
end 

回答by Mars Redwyne

What I have found using ActiveAdmin 0.6.1 is that ActiveAdmin will look for helpers in app/helpers/active_admin/*_helper.rb, but the name doesn't really matter.

我在使用 ActiveAdmin 0.6.1 时发现,ActiveAdmin 将在 app/helpers/active_admin/*_helper.rb 中寻找帮助程序,但名称并不重要

What does matter is:

重要的是:

  1. the filename must end in "_helper.rb"
  2. the module name must be the camel-case of the file name
  3. the file must be in app/helpers/active_admin/ directory.
  1. 文件名必须以“_helper.rb”结尾
  2. 模块名称必须是文件名的驼峰式
  3. 该文件必须在 app/helpers/active_admin/ 目录中。

If anyone knows where this is officially documented, that would be awesome.

如果有人知道这是官方记录的地方,那就太棒了。

Here is an example: https://gist.github.com/afred/7035a657e8ec5ec08d3b

这是一个例子:https: //gist.github.com/afred/7035a657e8ec5ec08d3b

回答by okliv

app/helpers/active_admin/view_helpers.rb

didn't help me

没有帮助我

EDITED: i changed it to views_helper.rb & ViewsHelper accordingly and it worked

编辑:我相应地将其更改为 views_helper.rb 和 ViewsHelper 并且它起作用了

*but if you want to define it only for certain resource, you can do it in my way

*但如果你只想为某些资源定义它,你可以按照我的方式来做



i had to define

我必须定义

#app/helpers/active_admin/categories_helper.rb

module ActiveAdmin::CategoriesHelper

  def helper_method

  end

end

for my active_admin resource app/admin/categories.rb

对于我的 active_admin 资源 app/admin/categories.rb

回答by Anh Nguyen

I can make it work in ActiveAdmin 0.6.1 (finally!). The solution is to create a helper module as following:

我可以让它在 ActiveAdmin 0.6.1 中工作(终于!)。解决方案是创建一个辅助模块,如下所示:

# app/helpers/active_admin_helpers.rb
module ActiveAdminHelpers
  # make this method public (compulsory)
  def self.included(dsl)
    # nothing ...
  end

  # define helper methods here ...
  def helper_method
    ...
  end
end

then include this module this way:

然后以这种方式包含此模块:

# app/admin/[resource].rb
include ActiveAdminHelpers

ActiveAdmin.register [Resource] do
  ...

end

Actually, it's not a nice solution but it's DRY and working good. I have already read and tried a lot of methods and solutions such as ViewHelpers module (put under 'app/helpers' or 'app/admin/active_admin'), ActiveAdmin::DSL monkey patching, ... but those never worked in version 0.6.1 (I don't have any ideas about other versions) :(

实际上,这不是一个很好的解决方案,但它是 DRY 并且运行良好。我已经阅读并尝试了很多方法和解决方案,例如 ViewHelpers 模块(放在“app/helpers”或“app/admin/active_admin”下)、ActiveAdmin::DSL 猴子补丁……但那些从未在版本中起作用0.6.1(我对其他版本没有任何想法):(

回答by Del

Another way to do this is to make the specific ActiveAdmin controller generated behind-the-scenes include the helper. This method will allow making the inclusion of the helpers explicit per file rather than global.

另一种方法是让特定的 ActiveAdmin 控制器在后台生成包含帮助程序。此方法将允许在每个文件中明确包含帮助程序而不是全局。

ActiveAdmin.register MyModel do
  controller do
    include MyHelper
  end
end

回答by tfischbach

Defining ActiveAdmin::ViewHelpersin app/admin/active_admin/view_helpers.rbworks for me with activeadmin 0.3.4and 0.5.0.

定义ActiveAdmin::ViewHelpersapp/admin/active_admin/view_helpers.rb对我的作品有activeadmin 0.3.40.5.0

回答by Leantraxxx

Using activeadmin 1.0.0.pre1 from git://github.com/activeadmin/activeadmin.git

使用来自 git://github.com/activeadmin/activeadmin.git 的 activeadmin 1.0.0.pre1

Rails 4.2.1

导轨 4.2.1

This worked for me...

这对我有用...

my_app/app/helpers/active_admin/resources_helper.rb

my_app/app/helpers/active_admin/resources_helper.rb

module ActiveAdmin
  module ResourcesHelper
    def resource_form_for(_resource, _params, _options = {}, &_block)
      url = if _resource.new_record?
              UrlBuilder.resources_path(_resource.class, _params)
            else
              UrlBuilder.resource_path(_resource.class, _params)
            end

      method = _resource.new_record? ? :post : :put

      options = { url: url, method: method, builder: ActiveAdmin::FormBuilder }
      options.merge!(_options)

      semantic_form_for([:admin, _resource], options) do |f|
        _block.call(f)
      end
    end
  end
end

my_app/app/admin/balance_sheets.rb

my_app/app/admin/balance_sheets.rb

ActiveAdmin.register BalanceSheet do
  form partial: 'form'
end

my_app/app/views/admin/balance_sheets/_form.html.erb

my_app/app/views/admin/balance_sheets/_form.html.erb

<%= resource_form_for(resource, params) do |f| %>
  <%= f.inputs "Fields" do %>
    <%= f.input :progress_status %>
    <%= f.input :crew %>
    <%= f.input :shift %>
    <%= f.input :expected_progress %>
    <%= f.input :real_progress %>
    <%= f.input :analyst, collection: User.analysts %>
    <%= f.input :activity_ids, as: :check_boxes, collection: Activity.balance_sheet_activities %>
    <%= f.input :worker_ids, as: :check_boxes, collection: Worker.all %>
  <% end %>
  <%= f.actions %>
<% end %>

回答by Luc Boissaye

You can also use ActiveAdmin partials :

您还可以使用 ActiveAdmin 部分:

render partial: 'admin/my_partial', locals: { var: my_var }

render partial: 'admin/my_partial', locals: { var: my_var }

And inside app/views/admin/_my_partial.html.arbyour active_admin ruby code.

app/views/admin/_my_partial.html.arb您的 active_admin ruby​​ 代码中。

回答by samsmith

What worked for me with Rails 3.2.11 and and gem activeadmin (0.5.1) was not adding the app/active_admin/view_helpers.rb file, or declaring any modules in config/initializers/active_admin.rb

对我有用的 Rails 3.2.11 和 gem activeadmin (0.5.1) 没有添加 app/active_admin/view_helpers.rb 文件,或者在 config/initializers/active_admin.rb 中声明任何模块

I put my helpers logically, by model, into the app/*_helpers.rb files. Then inside the app/admin/model.rb file I used:

我将我的助手按模型逻辑地放入 app/*_helpers.rb 文件中。然后在我使用的 app/admin/model.rb 文件中:

# app/admin/[resource].rb
ActiveAdmin.register [Resource] do
  ...
  filter :gender, as: :select, collection: proc{genders}
  ...
end

To use the helper in filters, to display a drop down list of genders to filter on, in the list view. For the corresponding create form fields, I used:

要在过滤器中使用助手,在列表视图中显示要过滤的性别下拉列表。对于相应的创建表单字段,我使用了:

# app/admin/[resource].rb
ActiveAdmin.register [Resource] do
  form do |f|
    f.inputs "Case Manager" do
      ...
      f.input :gender, as: :radio, collection: genders
      ...
      f.buttons
    end
  end
end

To display radio buttons for the input form.

显示输入表单的单选按钮。

Not sure why the proc{}is required outside of the form do |f|block, but if anyone can explain why it's a bad idea, I'll find a different way.

不知道为什么proc{}需要在form do |f|块外,但如果有人能解释为什么这是一个坏主意,我会找到一种不同的方式。