【问题标题】:rails duplicate record not rendering new轨道重复记录不呈现新的
【发布时间】:2011-12-18 22:51:26
【问题描述】:

我想要的只是复制现有记录。它应该使用填充的数据呈现新表单并让我“创建”这条新记录。

def clone
  @agreement = Agreement.find_by_slug!(params[:id])
  @agreement.clone

  respond_to do |format|
    format.html { render action: "new", notice: 'Agreement was successfully cloned.' }
  end
end

我的模特

def clone
  self.dup()
  self.slug = nil
end

我收到此错误:

No route matches {:action=>"show", :controller=>"agreements", :format=>nil, :id=>#<Agreement id: 1, date: "2011-12-18",...`

路线

resources :agreements do
  member do
    post 'approve'
    get 'clone', :controller => 'agreements', :action => 'clone'
  end
end

【问题讨论】:

    标签: ruby-on-rails-3 clone duplicate-data


    【解决方案1】:

    我认为你的克隆方法应该是:

    def clone
       clone = self.dup()
       clone.slug = nil
       clone
    end
    

    还有控制器:

    agreement = Agreement.find_by_slug!(params[:id])
    @agreement = agreement.clone
    

    ps:为什么要在路由中指定控制器和操作。这是默认值,还是我遗漏了什么?

    【讨论】:

    • 感谢@Robin 错误消失了。克隆现在有效。知道如何包含 has_many 关联吗?我还删除了路线中的绒毛,上一期的遗产
    • 一段时间以来,我一直在努力寻找这个问题的好答案。这是我找到的最好的一个。加上它的工作原理!
    猜你喜欢
    • 2017-07-19
    • 1970-01-01
    • 2020-06-07
    • 2012-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多