简体   繁体   English

创建关联后控制器中的Ruby on Rails NoMethodError

[英]Ruby on rails NoMethodError in controller after create an association

I have two models User and Promotion, an user can create has_many promotion and an promotion belong to user so : 我有两个模型User和Promotion,一个用户可以创建has_many促销,一个促销属于user,所以:

promotion.rb promotion.rb

class Promotion < ActiveRecord::Base
belongs_to :user
belongs_to :good

validates :name,  :presence => true
validates :title, :presence => true
validates :description, :presence => true


end

for the users i used devise so: 对于我使用过的用户,设计如下:

user.rb 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

now when i want create a new promotions get this error 现在,当我要创建新的促销时,会出现此错误

NoMethodError in PromotionsController#create undefined method `promotions' for nil:NilClass PromotionsController#Noil中的NoMethodError为nil:NilClass创建未定义的方法“ promotions”

this is the controller: 这是控制器:

 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

help please :) 请帮助 :)

It looks as though params[:user_id] did not contain a valid user id. 看起来params [:user_id]没有包含有效的用户ID。 Since you used find_by_id instead of find, it quietly assigned nil to @user, and of course nil doesn't have a method named #promotions, so that line failed. 由于您使用的是find_by_id而不是find,因此它悄悄地将nil分配给@user,当然nil没有名为#promotions的方法,因此该行失败了。

You need to either check for @user being nil, or change User.find_by_id to User.find and then rescue ActiveRecord::RecordNotFound. 您需要检查@user是否为nil,或将User.find_by_id更改为User.find,然后抢救ActiveRecord :: RecordNotFound。 In either case, respond with a custom 404 or whatever other way seems appropriate. 无论哪种情况,都可以使用自定义404或其他合适的方式进行响应。

One other question, is it your intention that a user can create promotions for any other user? 另一个问题是,您是否打算让某个用户可以为其他任何用户创建促销? If they should only be creating promotions for themselves, you can avoid this whole mess by just eliminating the whole User.find_by_id line, and changing the next line to: 如果他们只为自己创建促销活动,则可以通过消除整个User.find_by_id行并将下一行更改为:来避免整个混乱

@promotion = current_user.promotions.create(params[:promotion])

Devise should have already current_user for you. Devise应该已经有current_user了。 In any case, you also need to handle what happens if the promotion cannot be created because there are validation errors in the user-supplied parameters. 无论如何,您还需要处理由于用户提供的参数中存在验证错误而无法创建促销的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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