我已经为创建新客户端创建了一个端点。
起初我创建了客户端并使用了事务(见下面的代码)。
它工作正常。
# Before add set_result
js_method :on_new_contact, <<-JS
function() {
var form = this.items.first().getForm();
if (form.isValid()) {
this.newContact(form.getFieldValues());
this.closeRes = 'create_user';
this.close();
}
}
JS
endpoint :new_contact do |params|
data_hash = params.to_hash
Client.transaction do
new_client = Client.new
data_hash.each do |field, value|
new_client.__send__("#{field}=", value)
end
if new_client.save!
flash :notice => "Client #{new_client.full_name} has been created"
else
flash :error => new_user.errors.full_messages.to_sentence
end # if
end # transaction
{:netzkeFeedback => @flash}
end # endpoint
在我添加了设置结果以从端点返回新的客户端 ID 之后。
它将新客户端 3 次保存到我的数据库中。
# After add set_result
js_method :on_new_contact, <<-JS
function() {
var form = this.items.first().getForm();
if (form.isValid()) {
this.newContact(form.getFieldValues(), function(value){
alert(form);
alert(value);
}, this);
this.closeRes = 'create_user';
this.close();
}
}
JS
endpoint :new_contact do |params|
data_hash = params.to_hash
Client.transaction do
new_client = Client.new
data_hash.each do |field, value|
new_client.__send__("#{field}=", value)
end
if new_client.save!
flash :notice => "Client #{new_client.full_name} has been created"
else
flash :error => new_user.errors.full_messages.to_sentence
end # if
end # transaction
{:netzkeFeedback => @flash, :set_result => new_client.id}
end # endpoint
我已经尝试找出问题所在。
当我将 set_result 与事务一起使用时,我发现它发生了。
如果我删除transaction
ORset_result
代码,它将正常工作。
那我怎样才能让它工作呢?