[英]Undefined Method when using current_user from Devise
I have just started using device. 我刚刚开始使用设备。 I set up everything according to the instructions: 1. Defined default url options in your environment files, 2.Set the routes 3. Inserted flash messages 4. Generated the views and user model. 我按照说明进行所有设置:1.在环境文件中定义默认的url选项,2.设置路由3.插入Flash消息4.生成视图和用户模型。
Sign up / Sign in etc. is working fine but when using the current_user helper from devise inside my controller like this (submissions_controller.rb): 注册/登录等工作正常,但是在我的控制器内部通过devise使用current_user帮助程序时,如下所示(submissions_controller.rb):
...
before_filter :authenticate_user!, :except => [:index, :show]
...
def new
@submit = current_user.Submit.new
respond_with(@submit)
end
def create
@submit = current_user.Submit.new(submit_params)
@submit.save
respond_with(@submit)
end
I am getting the following error: 我收到以下错误:
NoMethodError in SubmitsController#new
undefined method `Submit' for #<User:0x007fea99a46f38>
Extracted source (around line #18):
def new
@submit = current_user.Submit.new
respond_with(@submit)
end
Any ideas what might cause this? 有什么想法可能导致这种情况吗?
I don't know what's the association between User
and Submit
, so 我不知道User
和Submit
之间有什么关联,所以
if user has many submits then change current_user.Submit.new
to 如果用户有很多提交,则将current_user.Submit.new
更改为
current_user.submits.new
else if user has one submit 否则,如果用户有一个提交
current_user.build_submit
Now using the above changes in your new
and create
actions 现在在您的new
动作和create
动作中使用以上更改
def new
@submit = current_user.submits.new
respond_with(@submit)
end
def create
@submit = current_user.submits.new(submit_params)
@submit.save
respond_with(@submit)
end
and your submit_params should look like 和您的submit_params应该看起来像
def submit_params
params.require(:submit).permit(:title, :url)
end
require(:submit)
is correct require(:submit)
是正确的
Hope it hepls! 希望它帮助!
You have this line of code 您有这行代码
@submit = current_user.Submit.new(submit_params)
It is failing here, because you are calling Submit
method on user
object, and you do not have Submit
method. 此处失败,因为您正在user
对象上调用Submit
方法,而您没有Submit
方法。 By looking at your code I see you wanted to create new Submit
. 通过查看您的代码,我看到您想要创建新的Submit
。
You should write 你应该写
@submit = Submit.new(submit_params) # instantiate new Submit object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.