开发者

How do I create a copy of a record in a controller?

I am trying to create a action that creates a 开发者_Go百科copy of the checked object.

My action so far:

  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

The action renders but no new webhost copy is created.


Try if that works (notice the create instead of new, since new by itself won't save it):

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


The 'new' method only instantiates a new object. It doesn't persist the object to your database (or whatever). You'll either have to call save on that object, or you could do

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

Calling create will instantiate a new object and save it (as long as it passes any validations).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜