Active Admin 中突然没有方法错误?

Posted

技术标签:

【中文标题】Active Admin 中突然没有方法错误?【英文标题】:Sudden no method error in Active Admin? 【发布时间】:2014-12-02 21:50:16 【问题描述】:

我安装了 activeadmin,并且在 may 应用程序的“评论”部分运行良好,允许我将个人评论添加到我的业务所在的各个位置。我尝试添加相同的设置,但使用 BusinessReviews 模型而不是评论(因此允许我在相同的基础上添加业务评论)

在我进入活动管理员之前一切正常(登录并访问“业务评论”面板很好,直到我尝试实际添加业务评论。然后我收到错误:

NoMethodError in Admin::BusinessReviews#new

undefined method `business_profile_image' for #<BusinessReview:0x007f893fe853d0>

我的模型如下:

    class BusinessReview < ActiveRecord::Base
belongs_to :location
  has_many :images, :as => :referrer
  accepts_nested_attributes_for :images, :allow_destroy => true
  def thumbnail
    if self.images.count > 0
      self.images.last.avatar(:thumb)
    else
      nil
    end
  end
end

my_app/admin/business_review.rb 如下:

    ActiveAdmin.register BusinessReview do
  index do
    column :business_reviewer_name
    column :business_review_content
    column :business_reviewer_address
    column :rating
    column :location do |business_review|
      business_review.location.name
    end
    column :business_profile_image do |business_review|
      image_tag(business_review.business_profile_image) if business_review.business_profile_image.present? 
    end
    actions
  end

  show do |business_review|
    attributes_table do
      row :business_reviewer_name
      row :business_profile_image
      row :business_review_content
      row :business_reviewer_address
      row :rating
      row :location do
        business_review.location.name
      end
    end
    panel "Images" do
      table_for business_review.images do
        column |img| img.currently_used 
        column |img| image_tag(img.avatar.url(:large)) 
      end
    end
    active_admin_comments
  end

  permit_params [:id, :business_reviewer_name, :business_profile_image, :business_review_content, :business_reviewer_address, :rating, :location_id], images_attributes: [:id,:_destroy,:avatar,:usage_type, :currently_used]

  form do |f|
    f.inputs 'Details' do
      f.input :business_reviewer_name
      f.input :business_profile_image
      f.input :business_review_content
      f.input :business_reviewer_address
      f.input :rating
      f.input :location
    end

    f.inputs "images" do
      f.has_many :images, :allow_destroy => true, :heading => 'Images', :new_record => true do |imgf|
        imgf.input :currently_used
        imgf.inputs "Attachment", :multipart => true do
          imgf.input :avatar, :as => :file, :hint => imgf.object.avatar? \
            ? imgf.template.image_tag(imgf.object.avatar.url(:large))
          : imgf.template.content_tag(:span, "no image yet")
        end
      end
    end
    f.actions
  end
end

我的架构的相关部分:

create_table "business_reviews", force: true do |t|
    t.text     "business_reviewer_content"
    t.string   "business_reviewer_name"
    t.string   "business_reviewer_address"
    t.float    "rating"
    t.string   "profile_image"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

路线好像也还好?

batch_action_admin_business_reviews POST       /admin/business_reviews/batch_action(.:format)    admin/business_reviews#batch
_action
             admin_business_reviews GET        /admin/business_reviews(.:format)                 admin/business_reviews#index
                                    POST       /admin/business_reviews(.:format)                 admin/business_reviews#creat
e
          new_admin_business_review GET        /admin/business_reviews/new(.:format)             admin/business_reviews#new
         edit_admin_business_review GET        /admin/business_reviews/:id/edit(.:format)        admin/business_reviews#edit
              admin_business_review GET        /admin/business_reviews/:id(.:format)             admin/business_reviews#show
                                    PATCH      /admin/business_reviews/:id(.:format)             admin/business_reviews#updat
e
                                    PUT        /admin/business_reviews/:id(.:format)             admin/business_reviews#updat
e
                                    DELETE     /admin/business_reviews/:id(.:format)             admin/business_reviews#destr
oy

我只是不明白,因为我设置的评论完美无缺并且完全相同(除了没有附加的业务_)。

【问题讨论】:

【参考方案1】:

根据您的架构,没有business_profile_image,只有profile_image:

t.string   "profile_image"

因此,要么重命名该列,要么使用 profile_image 而不是 business_profile_image。

【讨论】:

以上是关于Active Admin 中突然没有方法错误?的主要内容,如果未能解决你的问题,请参考以下文章

Active Admin成功登录重定向到用户登录页面

如何使用 Active Admin 执行批量操作

ac86u无法开机

django 管理员加载错误:/admin/login/ 处的 ImportError 没有名为后端的模块

Active Admin表单仅显示“# ”

Rails 3 - Active_admin 可以使用现有的用户模型吗?