2011-08-21 49 views
1

我想創建一個操作,創建一個檢查對象的副本。如何在控制器中創建記錄副本?

我的行動迄今:

def create_multiple 
    @webhost = Webhost.find(params[:webhost_ids]) 
    @webhost.each do |webhost| 
    Webhost.new(:webhost) 
    end 
    respond_to do |format| 
     format.html { redirect_to(:admin_webhosts, :notice => 'Konkurrancerne er nu slettet') } 
     format.xml { head :ok } 
    end 
end 

動作渲染,但不創建新的虛擬主機提供商的副本。

回答

1

試試是否工作(通知create代替new,因爲new本身不會保存):

def create_multiple 
    @webhost = Webhost.find(params[:webhost_ids]) 
    @webhost.each do |webhost| 
    Webhost.create(webhost.attributes) 
    end 
    respond_to do |format| 
     format.html { redirect_to(:admin_webhosts, :notice => 'Konkurrancerne er nu slettet') } 
     format.xml { head :ok } 
    end 
end 
1

'new'方法只實例化一個新的對象。它不會將對象持久保存到數據庫(或其他)。你要麼必須調用保存對象,或者(因爲它經過任何驗證,只要)你可以做

def create_multiple 
    @webhost = Webhost.find(params[:webhost_ids]) 
    @webhost.each do |webhost| 
    Webhost.create(webhost.attributes) 
    end 
    respond_to do |format| 
     format.html { redirect_to(:admin_webhosts, :notice => 'Konkurrancerne er nu slettet') } 
     format.xml { head :ok } 
    end 
end 

調用創建將實例化一個新的對象,並將其保存。

+0

現在,它創建一個新的虛擬主機提供商,但沒有數據:webhost_ids是ID檢查的虛擬主機 –

+0

對不起,編輯。這應該夠了吧 – dogenpunk