Rails 协会/会议问题

发布于 2024-11-03 11:00:00 字数 475 浏览 4 评论 0原文

我正在开发一款应用程序,供我的孩子们记录他们的家务活。我有 3 个孩子(Nick、Siena、Mac),并且有一个主页,每个名字都带有超链接...

我有以下关联:

Child:
  has_many :completions
  has_many :chores, :through=>:completion

Completion: 
  belongs_to :child
  belongs_to :chore

Chore:
  has_many :completions
  has_many :kid, :through=>:completion
  1. 如何(单击孩子的名字后)将 child_id 保存为会话对象将补全信息发布到补全模型?

  2. 当另一个孩子在主页中点击他们的名字时,如何清除/更改该会话到新的孩子?

非常感谢任何帮助。谢谢,CB

I am working on a app for my kids to log their chores. I have 3 children (Nick, Siena, Mac) and have a home page with each name hyperlinked ...

I have the following associations:

Child:
  has_many :completions
  has_many :chores, :through=>:completion

Completion: 
  belongs_to :child
  belongs_to :chore

Chore:
  has_many :completions
  has_many :kid, :through=>:completion
  1. How do I (upon clicking the child's name) save the child_id as a session object for posting completions to the completions models?

  2. How do I clear / change that sesison to a new child when another child clicks their name in the homepage?

Any help is greatly appreciated. Thanks, CB

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

向地狱狂奔 2024-11-10 11:00:00

所以乔希超越了。作为一个菜鸟,我问的问题对于大多数人来说更加简单和基本。答案非常简单:

ApplicationController

private
  def current_child
    child = Child.find(params[:id])
    session[:child_id] = child.id
  end
end

这允许我将孩子的 ID 存储在会话中并发布到已完成的模型中。

So josh went above and beyond. As a noob i was asking something much more simple and elementary for most folks. The answer was quite simple:

ApplicationController

private
  def current_child
    child = Child.find(params[:id])
    session[:child_id] = child.id
  end
end

This allowed me to store that child's id in a session and post to the completed model.

辞取 2024-11-10 11:00:00

如果我正确理解了这个问题(至少按照您的评论中的描述),我最近做了类似的事情。请参阅从多个父对象构建嵌套属性。基本上,我使用 polymorphic_url 建立一个链接来创建一个新项目(我可能会使用 @child.chores.build(params))并传递属性:chore_id,即

link_to "Mark as complete", polymorphic_url([:new, @child, :completion], :chore_id => @chore.id)

在您的控制器中确保您的 ChoresController#new 具有 希望这会有所

def new
  @chore = <current_child>.chores.build(params)
end

帮助。

If I understand the question correctly (at least as described in your comment), I did something similar recently. See Building a nested attribute from multiple parent objects. Basically, I used polymorphic_url to make a link to create a new item (I would probably use @child.chores.build(params)) and pass the attribute :chore_id, i.e.

link_to "Mark as complete", polymorphic_url([:new, @child, :completion], :chore_id => @chore.id)

In your controller make sure that for your ChoresController#new you have something like

def new
  @chore = <current_child>.chores.build(params)
end

Hope this helps.

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