问题:
当我编辑嵌套模型的内容时,它会保存新条目,而不是编辑已存在的条目。
模特:
# job.rb
class Job < ActiveRecord::Base
# Relations
has_many :logs, :dependent => :destroy
accepts_nested_attributes_for :logs, allow_destroy: true, reject_if: proc { |a| a['user_id'].blank? }
end
# log.rb
class Log < ActiveRecord::Base
belongs_to :job
belongs_to :user
# a single user should not be logged more than once per job
validates :user_id, uniqueness: { scope: :job_id }
end
# user.rb
class User < ActiveRecord::Base
has_many :logs
validates_presence_of :name
def self.all_active
User.where( active: 1 )
end
end
作业控制器:
class JobsController < ApplicationController
before_action :set_job, only: [:show, :edit, :update, :destroy]
def index
@jobs = Job.all
end
def show
end
def new
@job = Job.new
10.times { @job.logs.build }
end
def edit
( @job.people - @job.logs.count ).times { @job.logs.build }
end
def create
@job = Job.new(job_params)
# Set the admin id
@job.logs.each do |log|
log.admin_id = current_admin.id
end
respond_to do |format|
if @job.save
format.html { redirect_to @job, notice: 'Job was successfully created.' }
format.json { render :show, status: :created, location: @job }
else
format.html { render :new }
format.json { render json: @job.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @job.update(job_params)
format.html { redirect_to @job, notice: 'Job was successfully updated.' }
format.json { render :show, status: :ok, location: @job }
else
format.html { render :edit }
format.json { render json: @job.errors, status: :unprocessable_entity }
end
end
end
def destroy
@job.destroy
respond_to do |format|
format.html { redirect_to jobs_url, notice: 'Job was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_job
@job = Job.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def job_params
params.require(:job).permit(:people, :logs_attributes => [:user_id])
end
end
工作表格
<%= form_for(@job) do |f| %>
<% if @job.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@job.errors.count, "error") %> prohibited this job from being saved:</h2>
<ul>
<% @job.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="row">
<div class="small-3 columns"><%= f.label :people %></div>
<div class="small-9 columns"><%= f.integer :people %></div>
</div>
<table>
<%= f.fields_for :logs do |builder| %>
<%= render 'logs/form', { f: builder } %>
<% end %>
</table>
<div class="actions">
<%= f.submit %>
</div>
Logs Partial:
<tr>
<td>
<%= f.label :user_id %><br>
<%= f.collection_select(:user_id, User.all_active, :id, :name, { :prompt => 'Select User' } ) %>
</td>
<td>
<%= f.label :performance %><br>
<%= f.number_field :performance %>
</td>
</tr>
创建工作正常。然后我去编辑条目。在作业编辑屏幕上,我看到包含所有日志以及一些剩余的空白日志条目行(由于作业控制器中的10.times @logs.build
)。
现在,在没有编辑任何内容的情况下,我点击提交。我收到以下错误:
1 error prohibited this war from being saved:
Logs user has already been taken
此外,显示原始条目,然后在它们下面是相同的确切条目重复,但由于错误“红色”。空白条目列表(从10次开始)不再显示。
但是,从编辑屏幕,如果我将所有日志的用户更改为其他用户,我将不会收到错误。它将改为使用那些新选择的用户创建新条目,而不是修改当前条目。
我希望我已经提供了足够的信息来解决这个问题!感谢
答案 0 :(得分:0)
因此,事实证明问题是由于未将日志的:id
参数列入白名单而造成的。
我改变了我的job_controller:
自:
def job_params
params.require(:job).permit(:people, :logs_attributes => [:user_id])
end
要:
def job_params
params.require(:job).permit(:people, :logs_attributes => [:id, :user_id])
end
这就是造成验证问题的原因!这可能太复杂了q / a来帮助任何人,但是嘿,它只是可能!