我正在创建2个模块,1)价目表,2)折扣清单。这两个模块在数据库中共享一个名为“relclili”的表,它将存储名为“CustomerCustomer”的客户ID,价目表“ListP”的id和折扣列表“的id”。当客户被添加到折扣或价格列表中时,记录将创建为relclili,但如果将客户添加到价目表中然后添加到折扣中,则不应创建新记录,而是修改记录。客户被添加到价目表时创建
我试图让它工作,但它在.js文件中显示错误 这是我得到的错误:
ActionView::Template::Error (undefined method `CodCliente' for true:TrueClass):
1: $("#myclientadder_<%= @relclili.CodCliente %>").modal('hide');
2:
3: $("#container_relclilis").prepend('<%= j render @relclili %>'); //Prepend before append
4: $("#relclili_<%= @relclili.id %>").hide().fadeIn(1000);
app/views/relclilis/create.js.erb:1:in `_app_views_relclilis_create_js_erb___1051626349__1016832438'
这是我的创作动作:
def create
if Relclili.exists?(CodCliente: relclili_params[:CodCliente])
@relclili = (Relclili.find_by(CodCliente: relclili_params[:CodCliente]).first.update(ListaP: relclili_params[:ListaP]))
respond_to do |format|
format.html { redirect_to @relclili, notice: 'Relclili was successfully created.' }
format.json { render :show, status: :created, location: @relclili }
format.js {flash.now[:notice] = 'El cliente se ha agregado de forma exitosa.'} #ajax
end
else
@relclili = Relclili.new(relclili_params)
respond_to do |format|
if @relclili.save
format.html { redirect_to @relclili, notice: 'Relclili was successfully created.' }
format.json { render :show, status: :created, location: @relclili }
format.js {flash.now[:notice] = 'El cliente se ha agregado de forma exitosa.'} #ajax
else
format.html { render :new }
format.json { render json: @relclili.errors, status: :unprocessable_entity }
format.js {flash.now[:alert] = 'Error al agregar el cliente.'} #ajax
end
end
end
end
这是我的create.js.erb
$("#myclientadder_<%= @relclili.CodCliente %>").modal('hide');
$("#container_relclilis").prepend('<%= j render @relclili %>'); //Prepend before append
$("#relclili_<%= @relclili.id %>").hide().fadeIn(1000);
$("#cliente_<%= @relclili.CodCliente %>").fadeOut(500, function(){
$(this).remove();
$(".child").remove();
});
$("#notice").html("<%= escape_javascript(render :partial => 'partials/flash' , :locals => { :flash => flash }).html_safe %>");
setTimeout(function(){
$('#notice').fadeIn("slow", function() {
$(this).create();
})
}, 1500);
答案 0 :(得分:0)
问题在于:
@relclili = (Relclili.find_by(CodCliente: relclili_params[:CodCliente]).first.update(ListaP: relclili_params[:ListaP]))
将上面的内容拆分为2行(一行获取,一行更新):
@relclili = (Relclili.find_by(CodCliente: relclili_params[:CodCliente]).first
@relclili.update(ListaP: relclili_params[:ListaP])) if @relclili.present?
您的代码失败的原因是因为update
命令不返回对象,而只是一个布尔说明更新是成功还是失败。因此它说undefined method CodCliente for true:TrueClass)