ruby on rails创建新条目

时间:2014-12-20 23:20:57

标签: ruby-on-rails ruby model-view-controller methods

我有一个使用Ruby on Rails开发的Web应用程序,我的所有CRUD操作都运行得很好。当我创建一个新项目时,会显示一个新表单,我可以填写新项目的所有字段。

由于有多个项目的字段相同,我想添加一个类似“复制”的操作,以便我可以从现有项目创建一个新项目,这将使所有表单条目相同,然后,我只需要对新项目进行微小的更改,并在数据库中进行更新。

这些是我的行为

class ProjectsController < ApplicationController

# GET /projects/1
# GET /projects.1.json
def index
  @projects = Project.all

  respond_to do |format|
    format.html # index.html.erb
    format.json { render :json => @projects }
  end
end

# GET /projects/1
# GET /projects/1.json
def show
  @project = Project.find(params[:id])

  respond_to do |format|
    format.html # show.html.erb
    format.json { render :json => @project }
  end
end

# GET /projects/new
# GET /projects/new.json
def new
  @project = Project.new

  respond_to do |format|
    format.html # new.html.erb
    format.json { render :json => @project }
  end
end

# GET /projects/1/edit
def edit
  @project = Project.find(params[:id])
end

# POST /projects
# POST /projects.json
def create
  @project = Project.new(params[:project])

  respond_to do |format|
    if @project.save
    format.html { redirect_to @project, :notice => 'Project was successfully created.' }
      format.json { render :json => @project, :status => :created, :location => @project }
    else
      format.html { render :action => "new" }
      format.json { render :json => @project.errors, :status => :unprocessable_entity }
    end
  end
end

# POST /projects/1
# PUT /projects/1.json
def update
  @project = Project.find(params[:id])

  respond_to do |format|
    if @project.update_attributes(params[:project])
      format.html { redirect_to @project, :notice => 'Project was successfully updated.' }
      format.json { head :ok }
    else
      format.html { render :action => "edit" }
      format.json { render :json => @project.errors, :status => :unprocessable_entity }
    end
  end
end

# DELETE /projects/1
# DELETE /projects/1.json
def destroy
  @project = Project.find(params[:id])
  @project.destroy

  respond_to do |format|
    format.html { redirect_to projects_url }
    format.json { head :ok }
  end
end
end

在这种情况下,我的“复制”或“复制”操作会如何?

2 个答案:

答案 0 :(得分:2)

我会尝试这样的事情:

# in the routes.rb
resources :projects do
  get 'dublicate', :on => :member
end

允许您构建指向dublicate操作的链接,如下所示:link_to('duplicate', dublicate_project_path(@project))

# in the controller
def dublicate
  existing = Project.find(params[:id])
  @project = existing.dup

  respond_to do |format|
    format.html { render :new } 
    format.json { render :json => @project }
  end
end

这将复制(请参阅:http://apidock.com/rails/ActiveRecord/Core/dup)现有项目的属性(没有id字段)到新项目中,而不是显示带有预先填充字段的新页面。

答案 1 :(得分:0)

这样的事情:

def duplicate
  old_project = Project.find(params[:id])
  attributes = old_project.attributes.except(:id, :created_at, :updated_at)
  @project = Project.new(attributes)
  render :new
end

或者也许:

def duplicate
  old_project = Project.find(params[:id])
  attributes = old_project.attributes.except(:id, :created_at, :updated_at)
  @project = Project.new(attributes)
  if @project.save
    redirect_to project_path
  else
    render :edit
  end
end

请注意,如果您使用的是strong_params,则可能需要单独指定参数以防止禁用属性错误。

正如评论中指出的,你