#<Product:0xbc177fc> - Rails 3 的未定义方法“电子邮件”

undefined method `email' for #<Product:0xbc177fc> - Rails 3

我遇到了这个问题,我一直在使用 Rails 3.2 和 ruby 2.1

我正在使用 ActiveAdminDevise 将模型添加到我的管理区域。

我已经像这样添加了我的产品模型:

rails g active_admin:resource Product

它在我的管理区域中创建了新菜单,并且工作正常。

但是当我尝试保存新产品时它抛出这个错误:

NoMethodError at /admin/products
undefined method `email' for #<Product:0xbc177fc>

这是我的 Product.rb 型号:

class Product < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, 
     :recoverable, :rememberable, :trackable, :validatable

# Setup accessible (or protected) attributes for your model
#attr_accessible :email, :password, :password_confirmation, :remember_me
belongs_to :category
has_many :line_items
has_many :orders, through: :line_items

validates_presence_of :category_id, :name, :price_cents
#attr_accessible :avatar
#has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
#validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/

attr_accessor :price

attr_accessible :category_id, :description, :image, :name, :price_cents, :upc_code, :price, :taxable

def price
  price_cents/100.0 if price_cents
end

def price= price
  self.price_cents = (price.to_f*100).round
end

end

我评论了 email 行,但我仍然收到此错误,有什么想法吗?

这是我的 Gemfile:

source 'https://rubygems.org'

gem 'rails', '3.2.12'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'


# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'
  gem 'zurb-foundation'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platforms => :ruby

gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails', '~> 2.3.0'

group :development do
  gem "better_errors"
  gem 'annotate'
  gem 'pry'
  gem 'sqlite3'
end

group :production do
  gem 'pg'
end

gem 'ember-rails'
gem 'ember-source', '~> 1.10'
gem 'handlebars-source', '~> 2.0.0'
gem 'active_model_serializers', '~>0.8.0'
gem 'filepicker-rails'

gem 'thin'
gem 'unicorn'
gem 'capistrano'
gem 'simple_form'

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'debugger'

# File upload extension
gem "paperclip", "~> 4.3"

# Admin
gem 'activeadmin'
undefined method `email' for #<Product:0xbc177fc>

错误的意思是,它在 product 对象上调用了 email 方法,但找不到它。要使 email 方法可用于 product 对象,您必须将 :email 添加到 Product 模型中的 attr_accessor 列表。

email 添加到您的 Product 模特的 attr_accessor 列表中:

attr_accessor :price, :email

此外,如果你想更新:email属性,那么你还需要将:email添加到attr_accessible列表中:

attr_accessible :category_id, :description, :image, :name, :price_cents, :upc_code, :price, :taxable, :email