使用ActiveAdmin和Friendly_id时ActiveRecord :: ReadOnlyRecord [英] ActiveRecord::ReadOnlyRecord when using ActiveAdmin and Friendly_id

查看:134
本文介绍了使用ActiveAdmin和Friendly_id时ActiveRecord :: ReadOnlyRecord的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在一个项目中开始使用ActiveAdmin,几乎所有功能都运行良好,但是将它与friendly_id gem结合使用时遇到了问题。我正在为表单提交ActiveRecord :: ReadOnlyRecord [我相信],因为其id为readonly的friendly_id属性:

I started using ActiveAdmin recently in a project and almost everything works great but I'm having a problem when using it in combination with the friendly_id gem. I'm getting ActiveRecord::ReadOnlyRecord thrown for my forms [i believe] because of the friendly_id attribute whose ID is readonly:

{"utf8"=>"âœ"",
"_method"=>"put",
"authenticity_token"=>"Rc5PmUYZt3BiLvfPQr8iCPPXlbfgjoe/n+NhCwXazNs=",
"space"=>{"name"=>"The Kosmonaut",
"address"=>"8 Sichovykh Striltsiv 24",
"email"=>"info@somedomain.com"},
"commit"=>"Update Space",
"id"=>"the-kosmonaut"}  <--- culprit

我猜这是罪魁祸首,因为这是一个只读属性,它不在我的表单中,而是在PATH中

I'm guessing the last line is the culprit as it's a readonly attribute, it's not in my form but rather in the PATH

http:// localhost:5000 / manage / spaces / the-kosmonaut /编辑

如何通过尝试更新ID来解决此问题?

How can I fix this from trying to update the ID?

从ActiveAdmin看起来像这样:

Form from in ActiveAdmin looks like this:

  form do |f|
    f.inputs "Details" do
      f.input :name
      f.input :address
      f.input :email
      f.input :phone
      f.input :website
    end
    f.inputs "Content" do
      f.input :description
      f.input :blurb
    end
    f.buttons
  end

更新:这也不起作用,所以它不是friendly_id吗?

UPDATE: This doesn't work either so it's not the friendly_id?

我尝试使用@watson的建议,该建议应该有效,但仍然出现相同的错误;-(

I tried using @watson's suggestion which should have worked but still got the same error ;-(

{"utf8"=>"âœ"",
 "_method"=>"put",
 "authenticity_token"=>"Rc5PmUYZt3BiLvfPQr8iCPPXlbfgjoe/n+NhCwXazNs=",
 "space"=>{"name"=>"The Kosmonaut 23"},
 "commit"=>"Update Space",
 "id"=>"6933"}

http:// localhost:5000 / manage / spaces / 6933 / edit

当我使用以下命令在控制台中检查记录时record.readonly吗?返回错误

When I check the record in the console with record.readonly? it returns false

更新更新:删除scope_以解决问题。

UPDATE UPDATE: removing the scope_to fixes the problem.

scope_to :current_user, :unless => proc{ current_user.admin? }

唯一的问题是我需要使用scope_来防止用户看到他们不拥有的记录。我的猜测是(因为我假设scope_to通常可与has_many一起使用)我的HABTM关联会引起一些怪异?即用户<-HABTM->空格?

Only problem is I need the scope_to to prevent users from seeing records they do not own. My guess is (as I'm assuming scope_to normally works with has_many) that my HABTM association causes some weirdness? Ie Users <-- HABTM --> Spaces?

推荐答案

如果您只想在前端使用友好的ID,而不必不必在Active Admin中关心它们,您可以为Active Admin控制器还原friendly_id gem的效果。

If you only want friendly ID's in the front end and don't care about them inside Active Admin, you can revert the effects of the friendly_id gem for your Active Admin controllers.

我不确切知道friendly_id如何覆盖 to_param 方法,但是如果按常规方式进行操作,则在您的所有Active Admin控制器中重新覆盖该方法即可解决该问题,例如:

I don't know exactly how friendly_id overrides the to_param method, but if it's doing it the normal way, re-overriding it inside all of your Active Admin controllers should fix it, e.g.:

ActiveAdmin.register Foobar do
  before_filter do
    Foobar.class_eval do
      def to_param
        id.to_s
      end
    end
  end
end

您甚至可以更好地创建基础Active Admin控制器 ActiveAdmin :: ResourceController 中的before过滤器,以便它自动继承到所有Active Admin控制器中。

Even better you could create a before filter in the base Active Admin controller ActiveAdmin::ResourceController so that it is automatically inherited into all your Active Admin controllers.

首先将过滤器添加到 config / initializers / active_ad min.rb 设置:

First add the filter to the config/initializers/active_admin.rb setup:

ActiveAdmin.setup do |config|
  # ...
  config.before_filter :revert_friendly_id
end

打开 ActiveAdmin :: ResourceController 并添加 revert_friendly_id 方法,例如通过在 config / initializers / active_admin.rb 的末尾添加以下内容:

The open up ActiveAdmin::ResourceController and add a revert_friendly_id method, E.g. by adding the following to the end of config/initializers/active_admin.rb:

ActiveAdmin::ResourceController.class_eval do
  protected

  def revert_friendly_id
    model_name = self.class.name.match(/::(.*)Controller$/)[1].singularize

    # Will throw a NameError if the class does not exist
    Module.const_get model_name

    eval(model_name).class_eval do
      def to_param
        id.to_s
      end
    end
  rescue NameError
  end
end

更新:我刚刚更新了最后一个代码示例,以处理没有相关模型的控制器(例如Active Admin Dashboard控制器)

Update: I just updated the last code example to handle controllers with no related model (e.g. the Active Admin Dashboard controller)

更新2:我刚刚尝试将上述hack与friendly_id gem一起使用,它似乎工作得很好:)

Update 2: I just tried using the above hack together with the friendly_id gem and it seems to work just fine :)

更新3::清理代码以使用添加Active的标准方法在过滤到基本控制器之前先进行管理员

Update 3: Cleaned up the code to use the standard way of adding Active Admin before filters to the base controller

这篇关于使用ActiveAdmin和Friendly_id时ActiveRecord :: ReadOnlyRecord的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆