0

とても簡単なことですが、何が問題なのかわかりません。多くの機能を持つモデル ユーザー (デバイス) がいます。次のようになります。

/アプリ/モデル/user.rb

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

      # Setup accessible (or protected) attributes for your model
      attr_accessible :first_name, :last_name, :email, :password, :date_of_birth, :gender, :password_confirmation
      # attr_accessible :title, :body

      validates_inclusion_of :gender, :in => ['female', 'male'], message: 'not selected'
      validates :first_name, presence: true
      validates :last_name, presence: true
      validates_date :date_of_birth, :on_or_before => lambda { Date.current }
    end

/app/model/feature.rb

class Feature < ActiveRecord::Base
  belongs_to :user
  attr_accessible :content, :hits, :icon, :images, :lable, :rank, :success, :user_id
end

しかし、Rails の魔法はまったく機能しません。

rails console
user = User.create(email: 'test@test.de', password: 'test', password_confirmation: 'test', first_name: 'Test', last_name: 'Test', date_of_birth: '1992-12-21 00:00:00.000000', gender: 'male')
feature = user.feature.create(content: 'First Feature', hits: 5, icon: 'icon-bell', images: null, lable: 'Bell', rank: 1, success: 3)

とりわけエラーを返します。

NoMethodError: undefined method `feature' for #<User:0x4b377b8>

非常に多くの投稿を読みましたが、間違いを見つけることができませんでした。誰かが私にヒントを与えることができますか、それは私が犯した愚かな間違いだと思います. ありがとう!

4

1 に答える 1

1

あなたがしなければなりません

feature = user.features.create(content: 'First Feature', hits: 5, icon: 'icon-bell', images: null, lable: 'Bell', rank: 1, success: 3)

ユーザーは has_many :features であるため、ユーザーの機能を作成するときは、feature の代わりに features と入力する必要があります

feature = user.features.create
于 2013-10-25T08:25:02.043 に答える