Error: undefined method `attach_resumes' for nil:NilClass
Error: undefined method `attach_resumes' for nil:NilClass
我有用户 table 我需要用户在 attach_resumes
table 上创建简历,我已经创建关系但显示此错误:
undefined method `attach_resumes' for nil:NilClass
下面我的代码哪里错了?
用户模型
class User< ActiveRecord::Base
has_one :attach_resumes
end
attach_resumes 型号
class AttachResume < ActiveRecord::Base
belongs_to :user
end
attach_resumes 控制器
def new
@attach_resume = AttachResume.new
end
def create
params.permit!
if @attach_resume = @user.attach_resumes.create(params[:attach_resume])
flash[:notice] = "Attached Resume successfully"
render "attachCV"
else
render "attachCV"
flash[:warning] = "Please Attach Only PDF"
end
end
如果有人找到解决方案就太好了
正如它所指定的,您正在调用 attach_resumes
上的 class 为零。检查 @user 在该部分是否包含任何值。
另外,附注:
您正在建立 one-to-one 关系,但在模型和控制器中都使用 one-to-many 复数形式。如果您真的想要 one-to-one,请将模型中的行更改为
has_one :attach_resume
也在你的控制器中
@user.attach_resume.create(params[:attach_resume])
如果你想要one-to-many,那么更新关系到
has_many :attach_resumes
模型应该是:
class User< ActiveRecord::Base
has_one :attach_resume
end
注意 :attach_resume
没有 s
因此,当您在控制器中创建 attach_resume
时,将是:
@user.create_attach_resume(params[:attach_resume])
你的联想是错误的。它必须是 has_one :attach_resume
因为 has_one
是单数。因此,您还必须将 attach_resume
模型定义为单数。
class User< ActiveRecord::Base
has_one :attach_resume
end
您可以参考协会here for your situation. And you can refer Active Record Association here.
可能您的@user
没有经过验证
用户验证过程:
helper/applecation_helper
def user
@user ||= User.find_by(user_id: session[:user_id])
end
& 然后使用 @user
希望能理解这一点
我有用户 table 我需要用户在 attach_resumes
table 上创建简历,我已经创建关系但显示此错误:
undefined method `attach_resumes' for nil:NilClass
下面我的代码哪里错了?
用户模型
class User< ActiveRecord::Base
has_one :attach_resumes
end
attach_resumes 型号
class AttachResume < ActiveRecord::Base
belongs_to :user
end
attach_resumes 控制器
def new
@attach_resume = AttachResume.new
end
def create
params.permit!
if @attach_resume = @user.attach_resumes.create(params[:attach_resume])
flash[:notice] = "Attached Resume successfully"
render "attachCV"
else
render "attachCV"
flash[:warning] = "Please Attach Only PDF"
end
end
如果有人找到解决方案就太好了
正如它所指定的,您正在调用 attach_resumes
上的 class 为零。检查 @user 在该部分是否包含任何值。
另外,附注: 您正在建立 one-to-one 关系,但在模型和控制器中都使用 one-to-many 复数形式。如果您真的想要 one-to-one,请将模型中的行更改为
has_one :attach_resume
也在你的控制器中
@user.attach_resume.create(params[:attach_resume])
如果你想要one-to-many,那么更新关系到
has_many :attach_resumes
模型应该是:
class User< ActiveRecord::Base
has_one :attach_resume
end
注意 :attach_resume
没有 s
因此,当您在控制器中创建 attach_resume
时,将是:
@user.create_attach_resume(params[:attach_resume])
你的联想是错误的。它必须是 has_one :attach_resume
因为 has_one
是单数。因此,您还必须将 attach_resume
模型定义为单数。
class User< ActiveRecord::Base
has_one :attach_resume
end
您可以参考协会here for your situation. And you can refer Active Record Association here.
可能您的@user
没有经过验证
用户验证过程:
helper/applecation_helper
def user
@user ||= User.find_by(user_id: session[:user_id])
end
& 然后使用 @user
希望能理解这一点