我正在为我的网站制作一份清单部分。我有一个名为佣金的模型,其中包含有关调试任务的数据。我需要做的是当创建一个新的佣金条目时,我需要创建一系列约30个将链接到它的佣金任务。一种预定义值的清单,供人们浏览和检查。最好的方法是什么?
以下是我的模型和控制器:
commission.rb
class Commission < ApplicationRecord
has_many :comtasks
belongs_to :project
belongs_to :user
accepts_nested_attributes_for :comtasks, allow_destroy: true
end
comtask.rb
class Comtask < ApplicationRecord
belongs_to :commission
belongs_to :user
end
commissions_controller.rb
class CommissionsController < ApplicationController
before_action :set_commission, only: [:show, :edit, :update, :destroy]
# GET /commissions
# GET /commissions.json
def index
@commissions = Commission.all
end
# GET /commissions/1
# GET /commissions/1.json
def show
end
# GET /commissions/new
def new
@commission = Commission.new
end
# GET /commissions/1/edit
def edit
end
# POST /commissions
# POST /commissions.json
def create
@commission = Commission.new(commission_params)
respond_to do |format|
if @commission.save
format.html { redirect_to @commission, notice: 'Commission was successfully created.' }
format.json { render :show, status: :created, location: @commission }
else
format.html { render :new }
format.json { render json: @commission.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /commissions/1
# PATCH/PUT /commissions/1.json
def update
respond_to do |format|
if @commission.update(commission_params)
format.html { redirect_to @commission, notice: 'Commission was successfully updated.' }
format.json { render :show, status: :ok, location: @commission }
else
format.html { render :edit }
format.json { render json: @commission.errors, status: :unprocessable_entity }
end
end
end
# DELETE /commissions/1
# DELETE /commissions/1.json
def destroy
@commission.destroy
respond_to do |format|
format.html { redirect_to commissions_url, notice: 'Commission was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_commission
@commission = Commission.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def commission_params
params.require(:commission).permit(:project_id, :user_id, :description, :objectname, :location, comtasks_attributes: [:id, :content, :notes])
end
end
感谢任何想法或建议。
答案 0 :(得分:0)
以下是这个想法,
def create
@commission = Commission.create!(commission_params)
# use create not new to generate @commission.id value
# so comtask records can use the id value as reference
create_comtasks_job
# for comtask create I put in other method
respond_to do |format|
if @commission.save
format.html { redirect_to @commission, notice: 'Commission was successfully created.' }
format.json { render :show, status: :created, location: @commission }
else
format.html { render :new }
format.json { render json: @commission.errors, status: :unprocessable_entity }
end
end
end
def create_comtasks_job
# loop 30 tasks / or manual as follow
@commission.comtasks.build(content: 'content1',notes:'notes1')
@commission.comtasks.build(content: 'content2',notes:'notes2')
end
确保您的模型与下面的示例有关系
为您的模型
class Commission < ActiveRecord::Base
has_many :comtasks
end
class Comtask < ActiveRecord::Base
belongs_to :commission
end