简体   繁体   English

Rails为nil:NilClass使用的未定义方法`user'

[英]Rails undefined method `user' for nil:NilClass

Hello everyone I am new to rails and for some reason I keep getting an error 大家好,我是Rails的新手,由于某种原因,我一直遇到错误

undefined method 'user' for nil:NilClass . undefined method 'user' for nil:NilClass

I have pasted an image of the error on the bottom and I posted the code below. 我在底部粘贴了错误的图像,并在下面发布了代码。

Image Error 影像错误

# registeredapps_controller.rb

class RegisteredappsController < ApplicationController

 def create
   @registeredapp = Registeredapp.new(registeredapp_params)
   @registeredapp.user = current_user

  unless RegisteredappPolicy.new(current_user, @registeredapp).create?
    flash[:alert] = "not authorized" 
    redirect_to user_registeredapps_path(current_user.id)
  end

  if @registeredapp.save
    flash[:notice] = "You successfully registered your app."
    redirect_to user_registeredapp_path(current_user.id, @registeredapp.id)
  else
    flash.now[:alert] = "There was an error registering your app. Please try again."
    render :new
  end
end

Policies (using Pundit Gem) 政策(使用权威宝石)
# registeredapp_policy.rb

class RegisteredappPolicy 

  def initialize(user, registeredapp)
    @registeredapp = registeredapp
    @user = user
  end

  def create?
    if @user.present?
     @user = @registerdapp.user
    else
     false
  end
end

Here are my models 这是我的模特

# models/user.rb

class User < ApplicationRecord

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable, :confirmable

  def avatar_url(size)
    gravatar_id = Digest::MD5::hexdigest(self.email).downcase
    "http://gravatar.com/avatar/#{gravatar_id}.png?s=#{size}"
  end

  has_many :registeredapps
end

# models/registeredapp.rb

class Registeredapp < ApplicationRecord
  belongs_to :user
  has_many :events
end

Your issue is that the instance variable @registeredapp is mispelled in the RegisteredappPolicy class under the create? 您的问题是实例变量@registeredappcreate?下的RegisteredappPolicy类中拼写错误create? method. 方法。

It currently is 目前是

def create?
  if @user.present?
    @user = @registerdapp.user
  else
    false
  end
end

whereas the variable defined in the initialize is @registeredapp you're missing an e 而在Initialize中定义的变量为@registeredapp您缺少e

@registerdapp is nil @registerdapp nil

@user = @registerdapp.user

use try 使用try

@user = @registerdapp.try(:user)

or 要么

def create?
  if @user.present? && @registerdapp.present?
    @user = @registerdapp.user
  else
    false
  end
end

NOTE: Also you are missing one end for if else in create? 注意: if elsecreate?是否还缺少另一end create? method 方法

Rails convention usually is to have the model name like: Rails约定通常是具有以下模型名称:

User

And its corresponding table name like: 及其对应的表名如下:

users

In your ActiveRecord::Registeredapp you have :user , check if you shouldn't have :users . 在您的ActiveRecord :: Registeredapp中,您具有:user ,检查是否不应该具有:users

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

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