我有JobDeliveryCost
的表单,用户可以在其中添加投放费用。每次用户添加一个时,都会创建一个附加字段来添加另一个字段。此刻,我有一个表格,我会显示字段。
-jdc_array=(@job.job_delivery_costs.any? ? [@job.job_delivery_costs,@new_delivery].flatten : [@new_delivery])
-jdc_array.each do |jdc|
= simple_form_for [:admin, @job, @new_delivery] do |f|
%tr
%td
= jdc.timing
= f.input :timing, collection: JobDeliveryCost::TIMINGS, :include_blank => "please select"
%td
= f.input :delivery_cost_id, collection: DeliveryCost.order(:title), :label_method => :title,:value_method => :id
%td
-if jdc.new_record?
=f.submit "add"
-else
%td
= jdc.cost_per_unit
= f.input :cost_per_unit
%td
= jdc.quantity
= f.input :quantity
不是在每个表单条目上方显示输入的值,而是如何让字段保持其值?
另外,我如何显示此
的值 = f.input :delivery_cost_id, collection: DeliveryCost.order(:title), :label_method => :title,:value_method => :id
因为它是DeliveryCost模型的子属性?
对于额外的enfo,我添加了我的控制器和相关模型
class Admin::JobDeliveryCostsController < ApplicationController
before_action :set_job
def index
# raise @job.inspect
if get_deliverable
@jdc_array=(@job.job_delivery_costs.any? ? [@job.job_delivery_costs,@new_delivery] : [@new_delivery])
# raise @jdc_array.inspect
@new_delivery = @deliverable.job_delivery_costs.build
end
set_job_delivery_cost
end
def create
if @job
@job_delivery_cost = JobDeliveryCost.new(job_delivery_cost_params)
@job_delivery_cost.job = @job
if @job_delivery_cost.quantity.nil?
@job_delivery_cost.quantity = 1
end
# raise @job_delivery_cost.inspect
if @job_delivery_cost.save
flash[:success] = "Delivery Costs Added"
else
flash[:error] = "Delivery Costs not Added"
end
else
flash[:error] = "Couldn't find the Job."
end
redirect_to admin_job_job_delivery_costs_path(@job)
end
def destroy
set_job_delivery_cost
if @job.present? && @job_delivery_cost.present?
@job_delivery_cost.destroy
flash[:success] = "Job delivery cost removed"
else
flash[:error] = "Couldn't find the record"
end
redirect_to admin_job_job_products_path(@job)
end
private
def set_job
@job = Job.find_by(id: params[:job_id])
end
def set_job_delivery_cost
@job_delivery_cost ||= JobDeliveryCost.find_by(id: params[:id])
end
def job_delivery_cost_params
params.require(:job_delivery_cost).permit!
end
def get_deliverable
return @deliverable if @deliverable
if params[:contact_id].present?
@deliverable = Contact.find_by(id: params[:contact_id])
elsif params[:client_id].present?
@deliverable = Client.find_by(id: params[:client_id])
elsif params[:job_id].present?
@deliverable = Job.find_by(id: params[:job_id])
end
@deliverable
end
end
delivery_cost.rb
# == Schema Information
#
# Table name: delivery_costs
#
# id :integer not null, primary key
# title :string(255)
# unit :string(255)
# cost_per_unit :float
# created_at :datetime
# updated_at :datetime
#
class DeliveryCost < ActiveRecord::Base
UNIT_DAY='day'
UNIT_HOUR='hour'
UNIT_MILE='mile'
UNITS=[UNIT_DAY,UNIT_HOUR,UNIT_MILE]
has_many :job_delivery_costs
has_many :jobs, through: :job_delivery_costs
validates :cost_per_unit, presence: true
validates :unit, inclusion: UNITS
validates :title, presence: true
before_destroy :survive_if_jobs
private
def survive_if_jobs
jobs.empty?
end
end
JobDeliveryCost
# == Schema Information
#
# Table name: job_delivery_costs
#
# id :integer not null, primary key
# job_id :integer
# delivery_cost_id :integer
# cost_per_unit :float
# quantity :integer
# timing :string(255)
# created_at :datetime
# updated_at :datetime
#
class JobDeliveryCost < ActiveRecord::Base
TIMING_INSTALL='install'
TIMING_BREAKDOWN='breakdown'
TIMINGS=[TIMING_INSTALL,TIMING_BREAKDOWN]
belongs_to :delivery_cost
belongs_to :job
validates :quantity, presence: true, numericality: {greater_than_or_equal_to:1}
validates :timing, inclusion: TIMINGS
#validates :cost_per_unit, presence: true
validate :validate_cost_per_unit
validate :check_associates
# validate :quantity_default
before_save :init
private
def check_associates
associated_object_exists DeliveryCost, :delivery_cost_id
associated_object_exists Job, :job_id
end
def validate_cost_per_unit
if delivery_cost and cost_per_unit.blank?
self.cost_per_unit=delivery_cost.cost_per_unit
end
return false if cost_per_unit.blank?
end
def init
if self.quantity.nil?
self.quantity = 1
end
end
end
答案 0 :(得分:0)
如果我理解正确,您将要使用javascript来解决您的问题。有一个很好的railscast讨论了嵌套模型表单,但它也向您展示了如何构建javascript以动态添加带链接的字段元素。
您很可能希望注册onChange事件(意味着用户更改了字段中的数据)而不是链接,以添加另一个字段。