rails重复记录不呈现新 [英] rails duplicate record not rendering new

查看:290
本文介绍了rails重复记录不呈现新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

respond_to do | format |
format.html {render action:new,注意:协议已成功克隆}
end
end

我的模型

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

我收到这个错误: / p>

 没有路由匹配{:action =>show,:controller =>协议,:format => nil,:id =>#<协议ID:1,日期:2011-12-18,... 

路线

 资源:协议做
会员做
帖子' '
get'clone',:controller => 'agreement',:action => 'clone'
end
end


解决方案

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

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

而控制器: / p>

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

ps:为什么在路由中指定控制器和操作。这是默认的,或者我缺少什么?


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

解决方案

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?

这篇关于rails重复记录不呈现新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆