Rails Admin NoMethodError in RailsAdmin::Main#dele

2019-09-06 17:51发布

问题:

I have model Order:

class Order < ActiveRecord::Base
  has_one :shipping_address
  has_and_belongs_to_many :books

  validates :first_name, :surename, :email, :street1, :country, :zipcode, presence: true
  validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
  validates :zipcode, numericality: true

  accepts_nested_attributes_for :shipping_address
end

and model Book:

class Book < ActiveRecord::Base

  DEFAULT_PRICE = 55.15
  NEXT_BOOK_PERCENT = 5

  has_and_belongs_to_many :pages
  has_and_belongs_to_many :orders

  validates :name, presence: {message: "Name can't be blank."}
  validates_length_of :name, minimum: 3, maximum: 12, message: "Sorry, we can't create             this book right now. Please contact us for further information."

  validate :same_letter_validation
  validate :validates_for_non_alphabetic


  before_save :compile

  #......
end

Also I have table books_orders (book_id, order_id)

When I try do delete order from RailsAdmin panel I get next error:

NoMethodError in RailsAdmin::Main#delete

undefined method `orders_books' for #

It says that error in this line:

- @abstract_model.each_associated_children(object) do |association, child|

回答1:

Have you defined that "orders_books" method anywhere in your code? If so, can you please add it to your question. If you haven't, then the root cause of your issue is just that, that you're calling the "orders_books" method but it is not yet defined

Given that you reference "#books_orders" in your question, I believe it likely that you just swapped "books_orders" and "orders_books" at some point in your code



回答2:

Thanks. It's bug of a Rails 4.1.1. I have update it to 4.1.4 and all works OK.