rails duplicate record not rendering new

廉价感情. 提交于 2019-12-25 02:15:51

问题


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

回答1:


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?



来源:https://stackoverflow.com/questions/8555488/rails-duplicate-record-not-rendering-new

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!