ユーザーとプロモーションの 2 つのモデルがあります。ユーザーは has_many プロモーションを作成でき、プロモーションはユーザーに属します。
プロモーション.rb
class Promotion < ActiveRecord::Base
belongs_to :user
belongs_to :good
validates :name, :presence => true
validates :title, :presence => true
validates :description, :presence => true
end
私が考案したユーザーのために:
user.rb
class User < ActiveRecord::Base
has_many :promotions ,:foreign_key => "user_id",
:dependent => :destroy
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable, :omniauth_providers => [:facebook]
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me,:provider,:uid,:address,:name,:surname,:supplier,:partita_iva,:state,
:gender ,:language,:bio,:work,:education
今、新しいプロモーションを作成したいときにこのエラーが発生します
PromotionsController の NoMethodError#nil:NilClass の未定義メソッド「promotions」を作成
これはコントローラーです:
def create
@user = User.find_by_id(params[:user_id])
@promotion =@user.promotions.create(:params[:promotion])
redirect_to promotion_patch(@promotion)
respond_to do |format|
if @promotion.save
format.html { redirect_to @promotion, notice: 'Promotion was successfully created.' }
format.json { render json: @promotion, status: :created, location: @promotion }
else
format.html { render action: "new" }
format.json { render json: @promotion.errors, status: :unprocessable_entity }
end
end
end
助けてください :)