ActiveAdmin 评论模型无法正常工作
我按照Rails 入门创建了一个博客。然后我按照此处的教程尝试 ActiveAdmin: http://activeadmin.info/documentation.html 。
它适用于帖子和标签模型(我登录并创建/添加/编辑内容),但不适用于评论模型。
rails generate active_admin:resource Comment
create app/admin/comments.rb
然后,当我尝试启动服务器时,我得到:
rails server=> Booting WEBrick
=> Rails 3.0.7 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server Exiting /usr/local/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin/namespace.rb:116:in
`find_or_build_resource': Tried to
register Comment as Comment but
already registered to Comment
(ActiveAdmin::ResourceMismatchError)
from
/usr/local/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin/namespace.rb:45:in
`register' from
/usr/local/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin.rb:141:in
`register' from
/Users/myusername/Projects/RoR/blog/app/admin/comments.rb:1:in
`<top (required)>' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:in
`load' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:in
`block in load' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in
`block in load_dependency' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:596:in
`new_constants_in' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in
`load_dependency' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:in
`load' from
/usr/local/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin.rb:185:in
`block in load!' from
/usr/local/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin.rb:185:in
`each' from
/usr/local/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin.rb:185:in
`load!' from
/usr/local/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin.rb:212:in
`routes' from
/Users/myusername/Projects/RoR/blog/config/routes.rb:2:in
`block in <top (required)>' from
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/routing/route_set.rb:233:in
`instance_exec' from
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/routing/route_set.rb:233:in
`draw' from
/Users/myusername/Projects/RoR/blog/config/routes.rb:1:in
`<top (required)>' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:in
`load' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:in
`block in load' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in
`block in load_dependency' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:596:in
`new_constants_in' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in
`load_dependency' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:in
`load' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:127:in `block in reload_routes!' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:127:in `each' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:127:in `reload_routes!' from
/usr/local/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin.rb:123:in
`block in setup' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/callbacks.rb:420:in
`_run_prepare_callbacks' from
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/callbacks.rb:40:in
`initialize' from
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:33:in
`new' from
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:33:in
`build' from
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:79:in
`block in build' from
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:79:in
`each' from
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:79:in
`inject' from
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:79:in
`build' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:162:in `app' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application/finisher.rb:35:in
`block in <module:Finisher>' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/initializable.rb:25:in `instance_exec' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/initializable.rb:25:in `run' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/initializable.rb:50:in `block in run_initializers' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/initializable.rb:49:in `each' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/initializable.rb:49:in `run_initializers' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:134:in `initialize!' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:77:in
`method_missing' from
/Users/myusername/Projects/RoR/blog/config/environment.rb:5:in
`<top (required)>' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in
`require' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in
`block in require' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in
`block in load_dependency' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:596:in
`new_constants_in' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in
`load_dependency' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in
`require' from
/Users/myusername/Projects/RoR/blog/config.ru:3:in
`block in <main>' from
/usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/builder.rb:46:in
`instance_eval' from
/usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/builder.rb:46:in
`initialize' from
/Users/myusername/Projects/RoR/blog/config.ru:1:in
`new' from
/Users/myusername/Projects/RoR/blog/config.ru:1:in
`<main>' from
/usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/builder.rb:35:in
`eval' from
/usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/builder.rb:35:in
`parse_file' from
/usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/server.rb:162:in
`app' from
/usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/server.rb:248:in
`wrapped_app' from
/usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/server.rb:213:in
`start' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/commands/server.rb:65:in `start' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/commands.rb:30:in
`block in <top (required)>' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/commands.rb:27:in
`tap' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/commands.rb:27:in
`<top (required)>' from
script/rails:6:in `require' from
script/rails:6:in `<main>'
您的帮助将不胜感激。
I created a blog by following the Getting Started with Rails precisely. And then I following the tutorial here to try ActiveAdmin: http://activeadmin.info/documentation.html.
It's working fine for the Post and Tag models (I logged in and created/add/edited stuff) but not the Comment model.
rails generate active_admin:resource Comment
create app/admin/comments.rb
Then when I try to start the server I get this:
rails server=> Booting WEBrick
=> Rails 3.0.7 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server Exiting /usr/local/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin/namespace.rb:116:in
`find_or_build_resource': Tried to
register Comment as Comment but
already registered to Comment
(ActiveAdmin::ResourceMismatchError)
from
/usr/local/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin/namespace.rb:45:in
`register' from
/usr/local/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin.rb:141:in
`register' from
/Users/myusername/Projects/RoR/blog/app/admin/comments.rb:1:in
`<top (required)>' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:in
`load' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:in
`block in load' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in
`block in load_dependency' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:596:in
`new_constants_in' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in
`load_dependency' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:in
`load' from
/usr/local/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin.rb:185:in
`block in load!' from
/usr/local/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin.rb:185:in
`each' from
/usr/local/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin.rb:185:in
`load!' from
/usr/local/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin.rb:212:in
`routes' from
/Users/myusername/Projects/RoR/blog/config/routes.rb:2:in
`block in <top (required)>' from
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/routing/route_set.rb:233:in
`instance_exec' from
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/routing/route_set.rb:233:in
`draw' from
/Users/myusername/Projects/RoR/blog/config/routes.rb:1:in
`<top (required)>' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:in
`load' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:in
`block in load' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in
`block in load_dependency' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:596:in
`new_constants_in' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in
`load_dependency' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:in
`load' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:127:in `block in reload_routes!' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:127:in `each' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:127:in `reload_routes!' from
/usr/local/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin.rb:123:in
`block in setup' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/callbacks.rb:420:in
`_run_prepare_callbacks' from
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/callbacks.rb:40:in
`initialize' from
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:33:in
`new' from
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:33:in
`build' from
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:79:in
`block in build' from
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:79:in
`each' from
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:79:in
`inject' from
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:79:in
`build' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:162:in `app' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application/finisher.rb:35:in
`block in <module:Finisher>' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/initializable.rb:25:in `instance_exec' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/initializable.rb:25:in `run' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/initializable.rb:50:in `block in run_initializers' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/initializable.rb:49:in `each' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/initializable.rb:49:in `run_initializers' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:134:in `initialize!' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:77:in
`method_missing' from
/Users/myusername/Projects/RoR/blog/config/environment.rb:5:in
`<top (required)>' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in
`require' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in
`block in require' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in
`block in load_dependency' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:596:in
`new_constants_in' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in
`load_dependency' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in
`require' from
/Users/myusername/Projects/RoR/blog/config.ru:3:in
`block in <main>' from
/usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/builder.rb:46:in
`instance_eval' from
/usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/builder.rb:46:in
`initialize' from
/Users/myusername/Projects/RoR/blog/config.ru:1:in
`new' from
/Users/myusername/Projects/RoR/blog/config.ru:1:in
`<main>' from
/usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/builder.rb:35:in
`eval' from
/usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/builder.rb:35:in
`parse_file' from
/usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/server.rb:162:in
`app' from
/usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/server.rb:248:in
`wrapped_app' from
/usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/server.rb:213:in
`start' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/commands/server.rb:65:in `start' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/commands.rb:30:in
`block in <top (required)>' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/commands.rb:27:in
`tap' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/commands.rb:27:in
`<top (required)>' from
script/rails:6:in `require' from
script/rails:6:in `<main>'
Your help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看起来像一个错误(ActiveAdmin 已经有自己的内置
Comment
模型/类):https: //github.com/gregbell/active_admin/issues/64可能的解决方法是在
app/admin/comments.rb< 中为您的
Comment
模型指定不同的名称/代码>:Look like a bug (ActiveAdmin has it's own built-in
Comment
model/class already): https://github.com/gregbell/active_admin/issues/64A possible workaround could be to give your
Comment
model a different name within inapp/admin/comments.rb
:尝试
在终端中运行,
添加以下行
app/admin/active_admin_comment.rb
编辑config/initializers/active_admin之后
,现在您有机会在应用程序中自定义评论 /admin/active_admin_comment.rb
但我仍然没有找到添加 :update 和 :destroy 操作的解决方案
try this
run in terminal
add a following lines in
app/admin/active_admin_comment.rb
after that edit config/initializers/active_admin
And now you have opportunity to customize a comments in app/admin/active_admin_comment.rb
but i still didn't find solution to add :update and :destroy actions
迪伦的回答是一个很好的开始。除了他的之外,我还需要覆盖控制器资源/集合方法。
app/admin/comments.rb
基本上,您只是覆盖继承资源控制器的默认值(https://github.com/josevalim/inherited_resources#overwriting-defaults)
(是的,即使在 2014 年,这仍然是一个问题1.0.0 预发布版本,但似乎他们想通过阅读 github issues 来修复它)
Dylan's answer is a great start. In addition to his, I needed to overwrite the controller resource/collection methods.
app/admin/comments.rb
Basically, you're just overwriting the defaults of inherited resource controller (https://github.com/josevalim/inherited_resources#overwriting-defaults)
(yes, this is still an issue in 2014 even in the 1.0.0 prerelease version, but it appears that they want to fix it from reading the github issues)
我在版本 1.0.0.pre 中遇到了同样的问题,但幸运的是,您可以使用 active_admin.rb 中的配置完全抑制 ActiveAdmin 评论,然后您不必再像上面提到的那样为自己的 Comment 模型添加别名。
config/initializers/active_admin.rb
它也在文档中列出: https://github.com/gregbell/active_admin/blob/master/docs/1-general-configuration.md#comments
I had the same problem with version 1.0.0.pre, but thankfully, you can suppress ActiveAdmin comments altogether with a config in active_admin.rb then you don't have to alias your own Comment model anymore as mentioned above.
config/initializers/active_admin.rb
It's also listed in the docs: https://github.com/gregbell/active_admin/blob/master/docs/1-general-configuration.md#comments