简体   繁体   English

Ruby on Rails创建新条目

[英]ruby on rails create new entry

I have a Web App that I developed using Ruby on Rails and all my CRUD actions are working just fine. 我有一个使用Ruby on Rails开发的Web应用程序,我所有的CRUD操作都可以正常工作。 When I create a new project, a new form is displayed and I can fill in all the fields for the new project. 当我创建一个新项目时,将显示一个新表单,并且我可以填写新项目的所有字段。

Since there are so many fields that are identical for multiple projects, I'd like to add an action like "duplicate" so that I can create a new project from an existing one, that would have all form entries the same, then I'll have only minor changes to make into the new project and update it in the database. 由于多个项目的多个字段完全相同,因此我想添加一个类似“ duplicate”的操作,以便可以从现有项目创建一个新项目,该项目的所有表单条目都相同,然后我只需对新项目进行少量更改,然后在数据库中进行更新。

These are my actions 这些是我的行动

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

How would my "duplicate" or "copy" action look like in this case? 在这种情况下,我的“重复”或“复制”操作会如何?

I would try something like this: 我会尝试这样的事情:

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

That allow you to build links to the dublicate action like in the views like this: link_to('duplicate', dublicate_project_path(@project)) 这样,您就可以像这样的视图中那样建立指向重复操作的链接: 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

That would duplicate (see: http://apidock.com/rails/ActiveRecord/Core/dup ) the attributes of the existing project (without the id field) into an new project and than show the new page with prepopuated fields. 这会将现有项目(没有id字段)的属性复制(请参阅: http : //apidock.com/rails/ActiveRecord/Core/dup )复制到新项目中,然后显示带有预填充字段的新页面。

What about something like this: 像这样的事情呢:

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

Or maybe: 或者可能:

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

Note that in if you are using strong_params you might have to assign the params individually to prevent a forbidden attributes error. 请注意,如果使用strong_params ,则可能必须分别分配参数,以防止出现禁止的属性错误。

As pointed out in the comments, you 正如评论中指出的那样,您

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM