Rails 3. 如何添加 ActiveAdmin 将使用的助手?
我正在创建一个供 Formtastic 使用的助手,但收到 未定义的局部变量或方法
错误。我不知道该把它放在哪里才能发挥作用。
我已经在 application_helper.rb 和 app/helpers/active_admin/view_helpers.rb 中尝试过
I'm creating a helper to be used by Formtastic but I get the undefined local variable or method
error. I don't know where to put it so it can work.
I already tried in the application_helper.rb and in app/helpers/active_admin/view_helpers.rb
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您可以按照您的尝试在 app/helpers/ 中定义它们,但您需要通过活动管理员的初始值设定项包含它们,如下所示:
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/active_admin/views_helper.rb
文件中例子:
You need to put your helper functions in
app/helpers/active_admin/views_helper.rb
fileExample:
我使用 ActiveAdmin 0.6.1 发现 ActiveAdmin 会在 app/helpers/active_admin/*_helper.rb 中查找助手,但名称并不重要。
重要的是:
如果有人知道官方记录在哪里,那就太棒了。
这是一个示例: https://gist.github.com/afred/7035a657e8ec5ec08d3b
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.
What does matter is:
If anyone knows where this is officially documented, that would be awesome.
Here is an example: https://gist.github.com/afred/7035a657e8ec5ec08d3b
没有帮助我
编辑:我将其更改为views_helper.rb & ViewsHelper 相应地并且它有效
*但是如果您只想为某些资源定义它,您可以按照我的方式来完成
我必须
为我的 active_admin 资源 app/admin/categories.rb定义
didn't help me
EDITED: i changed it to views_helper.rb & ViewsHelper accordingly and it worked
*but if you want to define it only for certain resource, you can do it in my way
i had to define
for my active_admin resource app/admin/categories.rb
另一种方法是让后台生成的特定 ActiveAdmin 控制器包含帮助程序。此方法将允许在每个文件中显式包含帮助程序,而不是全局的。
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 0.6.1 中工作(终于!)。解决方案是创建一个辅助模块,如下所示:
然后以这种方式包含此模块:
实际上,这不是一个很好的解决方案,但它很干燥并且工作良好。我已经阅读并尝试了很多方法和解决方案,例如 ViewHelpers 模块(放在“app/helpers”或“app/admin/active_admin”下)、ActiveAdmin::DSL 猴子修补,...但那些从未在版本中工作过0.6.1(我对其他版本没有任何想法):(
I can make it work in ActiveAdmin 0.6.1 (finally!). The solution is to create a helper module as following:
then include this module this way:
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) :(
在
app/admin/active_admin/view_helpers.rb
中定义ActiveAdmin::ViewHelpers
适用于activeadmin 0.3.4
和0.5。 0 。
Defining
ActiveAdmin::ViewHelpers
inapp/admin/active_admin/view_helpers.rb
works for me withactiveadmin 0.3.4
and0.5.0
.使用来自 git://github.com/activeadmin/activeadmin.git 的 activeadmin 1.0.0.pre1
Rails 4.2.1
这对我有用...
my_app /app/helpers/active_admin/resources_helper.rb
my_app/app/admin/balance_sheets.rb
my_app/app/views/admin/balance_sheets/_form.html.erb
Using activeadmin 1.0.0.pre1 from git://github.com/activeadmin/activeadmin.git
Rails 4.2.1
This worked for me...
my_app/app/helpers/active_admin/resources_helper.rb
my_app/app/admin/balance_sheets.rb
my_app/app/views/admin/balance_sheets/_form.html.erb
您还可以使用 ActiveAdmin 部分:
renderpartial: 'admin/my_partial', locals: { var: my_var }
以及
app/views/admin/_my_partial.html.arb
你的 active_admin ruby 代码。You can also use ActiveAdmin partials :
render partial: 'admin/my_partial', locals: { var: my_var }
And inside
app/views/admin/_my_partial.html.arb
your active_admin ruby code.Rails 3.2.11 和 gem activeadmin (0.5.1) 对我有用的不是添加 app/active_admin/view_helpers.rb 文件,或者在 config/initializers/active_admin.rb 中声明任何模块
我逻辑地放置了我的助手,通过模型,进入 app/*_helpers.rb 文件。然后在 app/admin/model.rb 文件中我使用:
在过滤器中使用帮助器,在列表视图中显示要过滤的性别下拉列表。对于相应的创建表单字段,我使用:
显示输入表单的单选按钮。
不确定为什么在
form do |f|
块之外需要proc{}
,但如果有人能解释为什么这是一个坏主意,我会找到一个不同的方式。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
I put my helpers logically, by model, into the app/*_helpers.rb files. Then inside the app/admin/model.rb file I used:
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:
To display radio buttons for the input form.
Not sure why the
proc{}
is required outside of theform do |f|
block, but if anyone can explain why it's a bad idea, I'll find a different way.