【问题标题】:Rails link_to questionRails link_to 问题
【发布时间】: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 />

.. 这不起作用,因为:

  1. 然后我丢失了 child_id... 我需要他们记录他们的家务
  2. 我被路由到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。对吗?
  • 由于这个问题主要围绕ChildChoreCompetion 模型之间的关系,您应该发布该代码。
  • @adam... 张贴在@josh... 上方的主列表和渲染可能正是需要的。嗯
  • 已提交评论作为解决方案。让我知道它是否有效。

标签: ruby-on-rails routing link-to


【解决方案1】:
link_to child.name, chores_path(:child_id => child.id)

然后你可以用params[:child_id]在action中获取子id

例如,链接将指向/chores?child_id=1

【讨论】:

  • 这似乎是一个......但请参阅我在上面提供的关联。 chores 模型中没有 child_id 字段。
  • @chris 不一定是这样。这将使child_id 成为您可以从chores_controller 中看到的查询字符串参数。
  • 宾果游戏.. 谢谢。我想,当他们“提交”完成的家务时,我只会做 params[:child_id] ??
  • 如果是表单,请考虑添加一个名为 child_id 的隐藏字段,其值为孩子的 id。那将是最简单的。
【解决方案2】:

我一般会为这个场景创建子路由。

routes.rb

resources :children do
  resources :chores
end

chores_controller.rb

def index
  @child = Child.includes(:chores).find_by_id(params[:child_id])
  @chores = @child ? @child.chores : Chores.all
end

现在你可以把你的链接写成:

link_to child.name, child_chores_path(child)

More on nested resources

【讨论】:

  • 我在 Chore 模型中没有 child_id 字段,所以这不起作用。我只需要家务索引(列表),这样孩子就可以单击“完成”,这将把家务记录在 ChoresController#index 中的 Completion Model.ActiveRecord::RecordNotFound 中找不到没有 ID Rails.root 的孩子:/用户/chris/rails_projects/kids_chores 应用程序跟踪 |框架跟踪 |完整跟踪 app/controllers/application_controller.rb:12:in current_child' app/controllers/application_controller.rb:18:in set_current_child' 请求参数:{"child_id"=>"1"}
  • 最简单...我只需要link_to重定向到家务索引并保存child_id,然后孩子将点击他/她完成的家务,并且chore_id和child_id将存储在完成模型中.
  • @chris 如果你需要 child_id 来保存一个家务,那么孩子和家务之间存在语义关系。您应该关联模型而不是手动连接字段。从长远来看,它将为您节省大量时间。
  • @chris 一项家务可以分配给多个孩子吗?如果不查看您的模型代码,这很难推断出来。
  • @adam.. 感谢您在这里与我在一起:'孩子:has_many :completion has_many :chores, :through=>:completion Completion: belongs_to :child belongs_to :chore Chore: has_many :completion has_many :孩子,:through=>:completion'
【解决方案3】:

由于看起来您只是想在 Children#show 视图中呈现杂务的主列表,也许您可​​以尝试在适当位置的 children/show.html.erb 中呈现 'chores/index'。只需确保在 ChildrenController#show 方法中设置了一个主 @chores 变量即可。不确定这是否是一个完整的解决方案,但这是我的贡献。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-09
    • 1970-01-01
    • 1970-01-01
    • 2015-11-04
    • 1970-01-01
    • 2013-07-08
    相关资源
    最近更新 更多