我有一个布尔属性为“facturar”的模型。我无法通过GUI或控制台将此属性设置为true。出于某种原因,我可以通过控制台将其设置为false,而不是GUI。如果我尝试更新模型的任何记录,就会发生这种情况。类似的属性“facturar_proveedor”,也可以在GUI和控制台中将布尔值设置为true或false。什么可能导致这个奇怪的错误?
相关代码
create_table "factura_items", force: :cascade do |t|
t.integer "factura_id"
t.integer "ordene_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "facturar"
t.integer "iva", limit: 8
t.integer "subtotal", limit: 8
t.boolean "facturar_proveedor"
t.integer "subcuenta_puc_id"
end
控制器
class FacturaItemsController < ApplicationController
before_action :set_factura_item, only: [:show, :edit, :update, :destroy]
# GET /factura_items
# GET /factura_items.json
def index
@search = FacturaItemSearch.new(params[:search])
authorize FacturaItem
@factura_items = @search.scope
@factura_items = FacturaItem.all
end
# GET /factura_items/1
# GET /factura_items/1.json
def show
@factura_items = FacturaItem.all
authorize @factura_item
@ordene = Ordene.all
@medio = Medio.all
end
# GET /factura_items/new
def new
@factura_item = FacturaItem.new
authorize @factura_item
end
# GET /factura_items/1/edit
def edit
authorize @factura_item
end
# POST /factura_items
# POST /factura_items.json
def create
@factura_item = FacturaItem.new(factura_item_params)
authorize @factura_item
@ordenes = Ordene.all
@medio = Medio.all
respond_to do |format|
if @factura_item.save
format.html { redirect_to @factura_item, notice: 'Factura item was successfully created.' }
format.json { render :show, status: :created, location: @factura_item }
else
format.html { render :new }
format.json { render json: @factura_item.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /factura_items/1
# PATCH/PUT /factura_items/1.json
def update
respond_to do |format|
authorize @factura_item
if @factura_item.update(factura_item_params)
format.html { redirect_to @factura_item, notice: 'Item de factura asociado con factura.' }
format.json { render :show, status: :ok, location: @factura_item }
else
format.html { render :edit }
format.json { render json: @factura_item.errors, status: :unprocessable_entity }
end
end
end
# DELETE /factura_items/1
# DELETE /factura_items/1.json
def destroy
@factura_item.destroy
authorize @factura_item
respond_to do |format|
format.html { redirect_to factura_items_url, notice: 'Factura item was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_factura_item
@factura_item = FacturaItem.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def factura_item_params
params.require(:factura_item).permit(:factura_id, :ordene_id, :medio_id, :revisado, :fecha_orden, :medida, :unidad, :costo_unidad, :total, :facturar, :cantidad, :facturar_proveedor, :sin_iva, :subcuenta_puc_id)
end
end
修改表单
<% content_for :title do %>Factura Item<% end %>
<h3>Asignar Item a Factura</h3>
<div class="form">
<%= simple_form_for @factura_item do |form| %>
<%= form.input :facturar, label: 'Facturar a Cliente' %>
<%= form.input :facturar_proveedor, label: 'Facturar a Proveedor' %>
<%= form.input :sin_iva %>
<%= form.association :factura, :label_method => :id, include_blank: true, label: "Factura" %>
<%= form.button :submit, 'Guardar', class: 'submit' %>
<% end %>
</div>
<%= link_to 'Regresar', factura_items_path %>
POST日志
Started GET "/factura_items/1/edit" for ::1 at 2015-10-26 22:10:57 -0500
Processing by FacturaItemsController#edit as HTML
Parameters: {"id"=>"1"}
FacturaItem Load (0.3ms) SELECT "factura_items".* FROM "factura_items" WHERE "factura_items"."id" = $1 LIMIT 1 [["id", 1]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Factura Load (0.8ms) SELECT "facturas".* FROM "facturas"
Rendered factura_items/edit.html.erb within layouts/application (47.8ms)
Rendered layouts/_navigation_links.html.erb (2.2ms)
Rendered layouts/_navigation.html.erb (3.7ms)
Rendered layouts/_messages.html.erb (0.1ms)
Completed 200 OK in 410ms (Views: 403.8ms | ActiveRecord: 2.6ms)
Started PATCH "/factura_items/1" for ::1 at 2015-10-26 22:11:36 -0500
Processing by FacturaItemsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"r1IUDwfqd4U48wvc07dSUjCI2ki/iQfHEu7QMr9peNIaDPimLqCVX6HPjCV+pytGOwV4jW1FSSguFK71g+re0g==", "factura_item"=>{"facturar"=>"1", "facturar_proveedor"=>"0", "sin_iva"=>"0", "factura_id"=>"1"}, "commit"=>"Guardar", "id"=>"1"}
FacturaItem Load (0.4ms) SELECT "factura_items".* FROM "factura_items" WHERE "factura_items"."id" = $1 LIMIT 1 [["id", 1]]
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
(0.2ms) BEGIN
(1.1ms) SELECT SUM("transaccions"."debito") FROM "transaccions"
SQL (2.5ms) INSERT INTO "transaccions" ("factura_item_id", "fecha", "debito", "subcuenta_puc_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["factura_item_id", 1], ["fecha", "2015-10-26"], ["debito", 1160000], ["subcuenta_puc_id", 128], ["created_at", "2015-10-27 03:11:36.359055"], ["updated_at", "2015-10-27 03:11:36.359055"]]
SQL (0.6ms) INSERT INTO "transaccions" ("factura_item_id", "fecha", "credito", "subcuenta_puc_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["factura_item_id", 1], ["fecha", "2015-10-26"], ["credito", 1000000], ["subcuenta_puc_id", 878], ["created_at", "2015-10-27 03:11:36.367100"], ["updated_at", "2015-10-27 03:11:36.367100"]]
SQL (0.7ms) INSERT INTO "transaccions" ("factura_item_id", "fecha", "credito", "subcuenta_puc_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["factura_item_id", 1], ["fecha", "2015-10-26"], ["credito", 160000], ["subcuenta_puc_id", 880], ["created_at", "2015-10-27 03:11:36.371111"], ["updated_at", "2015-10-27 03:11:36.371111"]]
Ordene Load (0.7ms) SELECT "ordenes".* FROM "ordenes" WHERE "ordenes"."id" = $1 LIMIT 1 [["id", 1]]
Medio Load (0.6ms) SELECT "medios".* FROM "medios" WHERE "medios"."id" = $1 LIMIT 1 [["id", 2]]
(1.8ms) COMMIT
Redirected to http://localhost:3000/factura_items/1
Completed 302 Found in 117ms (ActiveRecord: 16.2ms)
Started GET "/factura_items/1" for ::1 at 2015-10-26 22:11:36 -0500
Processing by FacturaItemsController#show as HTML
Parameters: {"id"=>"1"}
FacturaItem Load (0.5ms) SELECT "factura_items".* FROM "factura_items" WHERE "factura_items"."id" = $1 LIMIT 1 [["id", 1]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Rendered factura_items/show.html.erb within layouts/application (4.0ms)
Completed 500 Internal Server Error in 14ms
NoMethodError - undefined method `id' for nil:NilClass:
app/views/factura_items/show.html.erb:3:in `_app_views_factura_items_show_html_erb__2824287575617260826_70230202524220'
actionview (4.2.0) lib/action_view/template.rb:145:in `block in render'
activesupport (4.2.0) lib/active_support/notifications.rb:166:in `instrument'
actionview (4.2.0) lib/action_view/template.rb:333:in `instrument'
actionview (4.2.0) lib/action_view/template.rb:143:in `render'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'
actionview (4.2.0) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument'
actionview (4.2.0) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:52:in `render_template'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:14:in `render'
actionview (4.2.0) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.2.0) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.2.0) lib/action_view/rendering.rb:100:in `_render_template'
actionpack (4.2.0) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.2.0) lib/action_view/rendering.rb:83:in `render_to_body'
actionpack (4.2.0) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.2.0) lib/action_controller/metal/renderers.rb:37:in `render_to_body'
actionpack (4.2.0) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.2.0) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
activesupport (4.2.0) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/Users/davefogo/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/benchmark.rb:303:in `realtime'
activesupport (4.2.0) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
activerecord (4.2.0) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:40:in `render'
actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.2.0) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.0) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `_run_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_process_action_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.0) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.0) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.0) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.0) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.2.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.0) lib/action_controller/metal.rb:236:in `block in action'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:42:in `serve'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:802:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.6.0) lib/rack/etag.rb:24:in `call'
rack (1.6.0) lib/rack/conditionalget.rb:25:in `call'
rack (1.6.0) lib/rack/head.rb:13:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.0) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.0) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.0) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:647:in `call'
activerecord (4.2.0) lib/active_record/migration.rb:378:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `_run_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_call_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
web-console (2.1.2) lib/web_console/middleware.rb:37:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.0) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.0) lib/rails/rack/logger.rb:20:in `call'
quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
actionpack (4.2.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.0) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.0) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.0) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.0) lib/rack/lock.rb:17:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/static.rb:113:in `call'
rack (1.6.0) lib/rack/sendfile.rb:113:in `call'
railties (4.2.0) lib/rails/engine.rb:518:in `call'
railties (4.2.0) lib/rails/application.rb:164:in `call'
rack (1.6.0) lib/rack/lock.rb:17:in `call'
rack (1.6.0) lib/rack/content_length.rb:15:in `call'
rack (1.6.0) lib/rack/handler/webrick.rb:89:in `service'
/Users/davefogo/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
/Users/davefogo/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
/Users/davefogo/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'
Started POST "/__better_errors/726ca75c50ed7ed5/variables" for ::1 at 2015-10-26 22:11:36 -0500
FacturaItem Load (0.8ms) SELECT "factura_items".* FROM "factura_items"
Ordene Load (1.1ms) SELECT "ordenes".* FROM "ordenes"
Medio Load (1.6ms) SELECT "medios".* FROM "medio
模型
class FacturaItem < ActiveRecord::Base
belongs_to :factura
belongs_to :medio
belongs_to :ordene
belongs_to :subcuenta_puc
has_many :incentivos
before_save :calculate_total
around_update :generate_transaccion_facturar_cliente, :if => :facturar?
#running 1, 2 ,3 => before update, 4 => after_update OK
before_update :generate_transaccion_facturar_proveedor, :if => :facturar_proveedor?
after_update :generate_transaccion_incentivo_automatico, :if => :facturar?
def calculate_total
self.subtotal = (costo_unidad * cantidad) - (costo_unidad * descuento)
self.iva = (subtotal * 0.16)
self.total = subtotal + iva
end
def generate_transaccion_facturar_cliente
#This callback creates a activo transaccions for the facturacion to a cliente.
self.subcuenta_puc_id = 128
Transaccion.create!(factura_item_id: self.id, fecha: Time.now, debito: self.total, subcuenta_puc_id: self.subcuenta_puc_id)
# generate_transaccion_facturar_cliente_pasivo
self.subcuenta_puc_id = 878
Transaccion.create!(factura_item_id: self.id, fecha: Time.now, credito: self.subtotal, subcuenta_puc_id: self.subcuenta_puc_id)
#generate_transaccion_factura_cliente_iva
self.subcuenta_puc_id = 880
self.iva = 0 if self.sin_iva == true
Transaccion.create!(factura_item_id: self.id, fecha: Time.now, credito: self.iva, subcuenta_puc_id: self.subcuenta_puc_id)
end
def generate_transaccion_facturar_proveedor
if cobro_proveedor == 'Facturacion'
#This callback creates an activo deudores transaccion for the facturacion to a proveedor for a incentivo.
self.subcuenta_puc_id = 129
Transaccion.create!(factura_item_id: self.id, fecha: Time.now, debito: self.subtotal, subcuenta_puc_id: self.subcuenta_puc_id)
#generate_transaccion_factura_proveedor_iva
self.subcuenta_puc_id = 742
#if self.sin_iva == true
#self.iva = 0
Transaccion.create!(factura_item_id: self.id, fecha: Time.now, credito: self.iva, subcuenta_puc_id: self.subcuenta_puc_id)
#Tgenerate_transaccion_facturar_proveedor_ingreso_operacional, his callback creates an ingreso operacional transaccion for the facturacion to a proveedor for a incentivo.
self.subcuenta_puc_id = 1232
Transaccion.create!(factura_item_id: self.id, fecha: Time.now, credito: self.subtotal, subcuenta_puc_id: self.subcuenta_puc_id)
end
end
def generate_transaccion_incentivo_automatico
if ordene.medio.tipo_de_volumen == 'Automatico'
#This callback creates an activo incentivos por cruzar
self.subcuenta_puc_id = 130
Transaccion.create!(factura_item_id: self.id, fecha: Time.now, debito: self.ordene.incentivo, subcuenta_puc_id: self.subcuenta_puc_id)
#This callback creates an ingreso operacional cruce, :generate_transaccion_incentivo_automatico_ingreso_operacional
self.subcuenta_puc_id = 1233
Transaccion.create!(factura_item_id: self.id, fecha: Time.now, credito: self.ordene.incentivo, subcuenta_puc_id: self.subcuenta_puc_id)
end
end
end
解决方案:我修改了模型中的回调。我删除了around_update回调并将其内容放在after_update中。
答案 0 :(得分:0)
你的PATCH日志似乎很奇怪,因为它没有做任何事情:
UPDATE "factura_items" SET "facturar" = $1, "updated_at" = $2 WHERE "factura_items"."id" = $3
并且有许多日志不是由您的控制器更新操作行为产生的,如:
(1.1ms) SELECT SUM("transaccions"."debito") FROM "transaccions"
SQL (2.5ms) INSERT INTO "transaccions" ("factura_item_id", "fecha", "debito", "subcuenta_puc_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["factura_item_id", 1], ["fecha", "2015-10-26"], ["debito", 1160000], ["subcuenta_puc_id", 128], ["created_at", "2015-10-27 03:11:36.359055"], ["updated_at", "2015-10-27 03:11:36.359055"]]
SQL (0.6ms) INSERT INTO "transaccions" ("factura_item_id", "fecha", "credito", "subcuenta_puc_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["factura_item_id", 1], ["fecha", "2015-10-26"], ["credito", 1000000], ["subcuenta_puc_id", 878], ["created_at", "2015-10-27 03:11:36.367100"], ["updated_at", "2015-10-27 03:11:36.367100"]]
SQL (0.7ms) INSERT INTO "transaccions" ("factura_item_id", "fecha", "credito", "subcuenta_puc_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["factura_item_id", 1], ["fecha", "2015-10-26"], ["credito", 160000], ["subcuenta_puc_id", 880], ["created_at", "2015-10-27 03:11:36.371111"], ["updated_at", "2015-10-27 03:11:36.371111"]]
答案 1 :(得分:0)
解决方案:我修改了模型中的回调。我删除了around_update回调并将其内容放在after_update中。