Rails 重复记录未呈现新记录

发布于 2024-12-21 17:45:14 字数 717 浏览 3 评论 0原文

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

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

all I want is duplicate an existing record. It should render new form with populated data and let me 'Create' this new record.

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

My Model

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

I get this error:

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

Routes

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贱人配狗天长地久 2024-12-28 17:45:14

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

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

和控制器:

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

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

I think your clone method should be:

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

And the controller:

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

ps: Why do you specify the controller and action in your routes. It's what the default would be, or am I missing something?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文