【发布时间】:2014-12-20 23:20:57
【问题描述】:
我有一个使用 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
在这种情况下,我的“复制”或“复制”操作会是什么样子?
【问题讨论】:
标签: ruby-on-rails ruby model-view-controller methods