【发布时间】:2011-04-29 00:47:29
【问题描述】:
我正在构建一个儿童杂务应用程序。在 application.html.erb 中,我显示了一个列出孩子姓名的侧栏:
<div id="side">
<%= link_to "Home", home_path %><br />
<% @children.each do |child| %>
<%= link_to child.name, child_path(child.id) %><br />
<% end %>
</div>
点击孩子的名字后,我希望显示所有家务。上面的代码将在点击时“显示”孩子。
我在想这样的事情:
<%= link_to child.name, chore_path %><br />
.. 这不起作用,因为:
- 然后我丢失了 child_id... 我需要他们记录他们的家务
- 我被路由到http://localhost:3000/chores/1(我只想要家务索引)
如何在本例中将 child_id 保留为变量但显示杂项索引?
干杯,克里斯
以下协会:
class Child < ActiveRecord::Base
has_many :completion, :dependent => :destroy
has_many :chores, :through => :completion
class Chore < ActiveRecord::Base
has_many :completions
has_many :kids, :through => :completions
class Completion < ActiveRecord::Base
belongs_to :child
belongs_to :chore
【问题讨论】:
-
每个孩子都有自己想要展示的家务吗?还是您希望每个孩子都能够从主列表中选择他们完成的家务?
-
听起来您几乎想在 Children#show 视图中呈现 Chores#index。对吗?
-
由于这个问题主要围绕
Child、Chore和Competion模型之间的关系,您应该发布该代码。 -
@adam... 张贴在@josh... 上方的主列表和渲染可能正是需要的。嗯
-
已提交评论作为解决方案。让我知道它是否有效。
标签: ruby-on-rails routing link-to