[英]Rails associations / sessions question
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...
我有 3 个孩子(尼克、锡耶纳、麦克),并且有一个主页,每个名字都带有超链接...
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
How do I (upon clicking the child's name) save the child_id as a session object for posting completions to the completions models?我如何(单击孩子的名字)将 child_id 保存为 session object 以将完成发布到完成模型?
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
谢谢,CB
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.这使我可以将该孩子的 ID 存储在 session 中并发布到完整的 model。
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, ie
基本上,我使用 polymorphic_url 来创建一个新项目的链接(我可能会使用@child.chores.build(params))并传递属性:chore_id,即
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在你的 controller 确保你的 ChoresController#new 你有类似的东西
def new
@chore = <current_child>.chores.build(params)
end
Hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.