Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 铁路协会/会议问题_Ruby On Rails_Model Associations - Fatal编程技术网

Ruby on rails 铁路协会/会议问题

Ruby on rails 铁路协会/会议问题,ruby-on-rails,model-associations,Ruby On Rails,Model Associations,我正在开发一个应用程序,让我的孩子记录他们的家务。我有3个孩子(尼克、锡耶纳、麦克),每个名字都有一个超链接的主页 我有以下协会: Child: has_many :completions has_many :chores, :through=>:completion Completion: belongs_to :child belongs_to :chore Chore: has_many :completions has_many :kid, :thro

我正在开发一个应用程序,让我的孩子记录他们的家务。我有3个孩子(尼克、锡耶纳、麦克),每个名字都有一个超链接的主页

我有以下协会:

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
  • 如何(单击孩子的名字)将孩子的id保存为会话对象,以便将完成发布到完成模型

  • 当另一个孩子在主页中单击他们的名字时,我如何清除/更改该sesison给一个新孩子


  • 非常感谢您的帮助。谢谢,CB

    如果我正确理解了这个问题(至少如您评论中所述),我最近也做了类似的事情。看见基本上,我使用多态url创建一个链接来创建一个新项目(我可能会使用@child.chores.build(params))并传递属性:chore_id,即

    link_to "Mark as complete", polymorphic_url([:new, @child, :completion], :chore_id => @chore.id)
    
    在你的控制器中,确保你的新舞蹈控制器有如下功能

    def new
      @chore = <current_child>.chores.build(params)
    end
    
    def新建
    @chore=.chores.build(params)
    结束
    

    希望这能有所帮助。

    所以乔希超越了一切。作为一个noob,我对大多数人的要求要简单得多。答案很简单:

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

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

    Rails路由与您的问题有何关系/这里有问题吗?看起来肯定不是这样。希望我能澄清这个问题。我需要将子id传递到下一个视图(琐事/索引)。。。因此,在选择琐事并单击submit。。子id和杂务id被传递给完成的模型。非常感谢,我很感激。我想我在问一些更简单的问题。。。我如何在点击孩子的名字时设置“当前孩子”呢?你说的更像是一个全局变量吗?或者这是在某个模型的上下文中吗?谢谢你的接受。你的解决方案将与我的下一个建议一致。很高兴这对你有用。也谢谢你,乔希。看来你是这个酷社区的一个很好的帮助/资源。