Rails,模型关系问题

发布于 2024-10-02 05:01:41 字数 1023 浏览 1 评论 0原文

我有以下效果很好的:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水水月牙 2024-10-09 05:01:41

错误消息准确地告诉您出了什么问题:@requestable 对象不存在 current_user,无论它是什么。

current_user 很可能是从 ApplicationController 继承的函数,或者至少通常是它所在的位置。它通常根据当前会话返回一个 User 对象。但这不是 Rails 的内置部分,因此如果您希望我更详细地介绍,我们需要更多信息。

@requestable 看起来像一个多态模型实例,因此它不会知道当前会话。


这里发生了一些 Rails 魔法,我认为这让您感到困惑。

@requestable.request_threadscurrent_user.request_threads 都是 Rails 在这些对象上生成的辅助函数,可通过自动过滤结果和填充值来节省您的时间。

我认为,根据您到目前为止向我们展示的代码,您正在尝试将 current_user 与您正在创建的新 request_thread 关联起来。在这种情况下,您可以简单地将其手动合并到属性中。因为我不知道你的模型是什么样的,所以我只能猜测字段名称:

@request_thread = @requestable.request_threads.new( params[:request_thread].merge(:user => current_user) )

就像我说的,可链接函数只是为了方便。例如,您可以这样写:

@request_thread = request_thread.new(params[:request_thread])
@request_thread.requestable = find_requestable
@request_thread.user = current_user

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 from ApplicationController, 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 and current_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 new request_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:

@request_thread = @requestable.request_threads.new( params[:request_thread].merge(:user => current_user) )

Like I said, the chainable functions are merely for convenience. For instance, you could write it this way instead:

@request_thread = request_thread.new(params[:request_thread])
@request_thread.requestable = find_requestable
@request_thread.user = current_user
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文