简体   繁体   中英

Sudden no method error in Active Admin?

I have activeadmin installed and working fine for a 'reviews' section of may app, allowing me to add individual reviews to various locations in which my business is based. I tried to add the identical set up but using a BusinessReviews model rather than Reviews (thus allowing me to add business reviews on the same basis)

Everything works fine until I go into active admin (log in and accessing the 'Business Reviews' panel is fine, until I try and actually add a business review. I then get the error:

NoMethodError in Admin::BusinessReviews#new

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

My model is as follows:

    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 is as follows:

    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

Relevant part of my schema:

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

The routes appear to be there ok too?

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

I just don't get it as the reviews one I set up works perfectly and is identical (apart from the not haveing business_ appended to it).

According to your schema there is no business_profile_image but just profile_image:

t.string   "profile_image"

So either rename the column or use profile_image instead of business_profile_image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM