未定义的方法`错误'为nil:Rails上的NilClass?

时间:2016-02-12 05:26:32

标签: ruby-on-rails

我正在

未定义的方法`错误'为零:NilClass

在创建新数据表之前进行自定义验证时。 这是我在控制器中的代码,在验证方法触发之前一切正常。

class ActivitiesController < ApplicationController
  layout 'admin'

  before_action :admin_logged_in_user, only: [:create, :destroy,:edit,:update]
  before_action :admin_correct_user, only:[:destroy,:update,:edit]
  before_action :permanent_event_check, only:[:create,:update]

  def list

  end

  def create
    @activity = admin_current_user.activities.build(activity_param)
    if @activity.save
      flash[:success] = "Activity Created!"
      redirect_to admin_dashboard_url
    else
      render 'new'
    end
  end

  def new
    @activity = Activity.new
  end

  private

  def activity_param
    params.require(:activity).permit(:name,:details,:start_at,:end_at,
                                     :activity_image01_url,:activity_image02_url,:activity_image03_url,
                                     :youtube_url,:capacity,:booking_status,:rules,:apply_details,
                                     :payment_price,:payment_need,:avaliable,:rating,:temple_id)
  end

  def admin_correct_user
    @activity = admin_current_user.activities.find_by(id: params[:id])
    redirect_to admin_dashboard_url if @activity.nil?
  end

  def permanent_event_check
    param_activity = params[:activity]

    if param_activity[:permanent] == "false"
      if param_activity[:start_at] == "" || param_activity[:end_at] == ""
        @activity.errors[:base] << "You can't leave start and end date blank with Permanent Event"
        return false
      end
    end

  end

end

这一行。

@activity.errors[:base] << "You can't leave start and end date blank with Permanent Event"

我从这一行得到了错误。

如何解决这个问题? 谢谢!

*******移动验证后添加模型文件

在我尝试移动验证后,这是我的模型,但似乎没有任何回复@activity

validate :permanent_event_check
private

  def permanent_event_check
    param_activity = @activity

    if param_activity.permanent == "false"
      if param_activity.start_at == "" || param_activity.end_at == ""
        @activity.errors[:base] << "You can't leave start and end date blank with Permanent Event"
        return false
      end
    end
  end

1 个答案:

答案 0 :(得分:0)

来自before_action区块:

before_action :admin_logged_in_user, only: [:create, :destroy,:edit,:update]
before_action :admin_correct_user, only:[:destroy,:update,:edit]
before_action :permanent_event_check, only:[:create,:update]

在您添加错误的地方admin_correct_user之前,@activity设置了permanent_event_check

但是,admin_correct_user仅触发操作:[:destroy,:update,:edit] permanent_event_check触发createupdate

这意味着,对于create操作,控制器中的事件顺序为:admin_logged_in_user(这可能是验证) - &gt; permanent_event_check(尝试添加错误)。请注意,已跳过旨在设置admin_correct_user的其他操作(@activity)。

因此......对于create操作,特别是在您尝试添加错误时,没有名为@activity的对象。

  

@activity是Nil

所以...错误的原因。

要修复它:

确保在尝试访问它之前构建@activity对象(用于验证或任何其他事物)。

Arup建议将验证转移到您的模型中是一个很好的建议!

将其移至模型:

#activity.rb:
validate :right_event

def right_event
  #check validity, and add custom error here
  unless permanent
    errors.add(:permanent, "You can't leave start and end date blank...") unless self.start_at and self.end_at
  end
end