Rails,模型关系问题
我有以下效果很好的:
@request_thread = current_user.request_threads.new(params[:request_thread])
我有这个效果很好的:
@requestable = find_requestable
@request_thread = @requestable.request_threads.new(params[:request_thread])
但是如果我尝试使用第二行:
@request_thread = @requestable.current_user.request_threads.new(params[:request_thread])
使用 current_user 我会收到以下错误:
NoMethodError (undefined method `current_user' for #<Photo:0x10f95c828>):
app/controllers/request_threads_controller.rb:52:in `create'
app/middleware/flash_session_cookie_middleware.rb:14:in `call'
我在这里搞砸了什么?
谢谢
更新 - 使用 find_requestable
# http://asciicasts.com/episodes/154-polymorphic-association
def find_requestable
params.each do |name, value|
if name =~ /(.+)_id$/
return $1.classify.constantize.find(value)
end
end
nil
end
I have the following which works great:
@request_thread = current_user.request_threads.new(params[:request_thread])
And I have this which works great:
@requestable = find_requestable
@request_thread = @requestable.request_threads.new(params[:request_thread])
But if I try the 2nd line with:
@request_thread = @requestable.current_user.request_threads.new(params[:request_thread])
With the current_user I get the following error:
NoMethodError (undefined method `current_user' for #<Photo:0x10f95c828>):
app/controllers/request_threads_controller.rb:52:in `create'
app/middleware/flash_session_cookie_middleware.rb:14:in `call'
What did I mess up on here?
Thanks
UPDATE - with find_requestable
# http://asciicasts.com/episodes/154-polymorphic-association
def find_requestable
params.each do |name, value|
if name =~ /(.+)_id$/
return $1.classify.constantize.find(value)
end
end
nil
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误消息准确地告诉您出了什么问题:
@requestable
对象不存在current_user
,无论它是什么。current_user
很可能是从ApplicationController
继承的函数,或者至少通常是它所在的位置。它通常根据当前会话返回一个 User 对象。但这不是 Rails 的内置部分,因此如果您希望我更详细地介绍,我们需要更多信息。@requestable
看起来像一个多态模型实例,因此它不会知道当前会话。这里发生了一些 Rails 魔法,我认为这让您感到困惑。
@requestable.request_threads
和current_user.request_threads
都是 Rails 在这些对象上生成的辅助函数,可通过自动过滤结果和填充值来节省您的时间。我认为,根据您到目前为止向我们展示的代码,您正在尝试将
current_user
与您正在创建的新request_thread
关联起来。在这种情况下,您可以简单地将其手动合并到属性中。因为我不知道你的模型是什么样的,所以我只能猜测字段名称:就像我说的,可链接函数只是为了方便。例如,您可以这样写:
The error message tells you exactly whats wrong:
current_user
doesn't exist for the@requestable
object, whatever that is.current_user
is most likely a function inherited fromApplicationController
, or at least that's usually where it lives. It usually returns a User object according to the current session. But that isn't a built-in part of Rails, so we need more information if you want me to go into greater detail.@requestable
looks like a polymorphic model instance so it wouldn't be aware of the current session.There's a bit of Rails magic happening here that I think is confusing you.
Both
@requestable.request_threads
andcurrent_user.request_threads
are helper functions that have been generated by Rails on those objects to save you time by filtering results and filling in values automatically.I think, by the code you have shown us so far, that you are trying to associate
current_user
with the newrequest_thread
you are creating. In that case, you can simple merge that into the attributes manually. Since I don't know what your models look like I can only guess at the field names:Like I said, the chainable functions are merely for convenience. For instance, you could write it this way instead: