0

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
mu is too short
  • 426,620
  • 70
  • 833
  • 800
Gaelle
  • 599
  • 2
  • 6
  • 24

1 Answers1

2

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?

Robin
  • 21,667
  • 10
  • 62
  • 85
  • Thanks @Robin Error gone. Cloning works now. Any idea how to include has_many associations as well? I also removed the fluff in my routes, heritage from previous issue – Gaelle Dec 19 '11 at 05:44
  • I have been trying to find a good answer to this question for a while. This is the best one I have found. Plus it works! – memoht Dec 05 '12 at 04:52