[英]Ruby On Rails callbacks don't work on Heroku, but work locally
I've been working on a school appplication using rails_admin. 我一直在使用rails_admin开发学校应用程序。 Everything was working fine locally but when I push to Heroku the callbacks I defined in my models are not used, not even the most simple. 一切都在本地工作正常但是当我推送到Heroku时,我在模型中定义的回调没有被使用,甚至不是最简单的。 So I come to you asking if someone has encountered a problem like this before or could help me interpret my logs because I cannot find the reason for this. 所以我来问你是否有人之前遇到过这样的问题,或者可以帮我解释我的日志,因为我找不到原因。
First, this is what my Gemfile looks like: 首先,这是我的Gemfile的样子:
source 'https://rubygems.org'
ruby '2.2.0'
group :development, :test do
gem 'railroady'
end
# For documenting models automatically
gem 'annotate', '~> 2.6.6'
# For styling all HTML using SASS
gem 'bourbon'
gem 'neat'
gem 'bitters'
# For using creating SQL triggers inside models
gem 'hairtrigger'
# For creating seed data files from existing data in the database
gem 'seed_dump'
# Used for translations of the mailer and account confirmations
gem 'devise-i18n'
# User permissions and login
gem 'devise'
gem 'cancancan', '~> 1.10'
gem "rolify"
gem "figaro"
gem "rails_admin"
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.1'
# Use sqlite3 as the database for Active Record on the development environment
gem 'sqlite3', group: :development
# Use PostgreSQL as the database for Active Record on the production environment
gem 'pg', group: :production
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring', group: :development
gem 'rails_12factor', group: :production
And this is the models I'm using callbacks in: 这是我使用回调的模型:
# == Schema Information
#
# Table name: regions
#
# id :INTEGER not null, primary key
# name :varchar
# key :varchar
# created_at :datetime not null
# updated_at :datetime not null
#
class Region < ActiveRecord::Base
has_many :events, :dependent => :destroy, :inverse_of => :region
has_and_belongs_to_many :staff_members
before_create :generate_key
rails_admin do
visible do
bindings[:controller].current_user.roles.first.name == "super_admin"
end
list do
field :id
field :name
field :key
end
edit do
field :name do
required true
help "Por favor llena este campo."
end
end
end
private
def generate_key
self.key = self.name[0..1]
end
end
# == Schema Information
#
# Table name: teams
#
# id :INTEGER not null, primary key
# name :varchar
# key :varchar
# date_of_registration :date
# company_name :varchar
# category_id :integer
# address_id :integer
# created_at :datetime not null
# updated_at :datetime not null
# winner :boolean
#
class Team < ActiveRecord::Base
belongs_to :category, :inverse_of => :teams
belongs_to :address, :inverse_of => :teams
has_many :evaluations, :dependent => :destroy, :inverse_of => :team
has_many :team_members, :dependent => :destroy, :inverse_of => :team
has_and_belongs_to_many :events
before_save :generate_key
def team_label_method
"#{self.key} - #{self.name}"
end
rails_admin do
object_label_method do
:team_label_method
end
list do
field :id do
column_width 40
end
field :name do
column_width 100
end
field :key do
column_width 90
end
field :company_name do
column_width 100
end
field :category do
column_width 70
end
field :events do
column_width 100
end
field :winner do
column_width 10
end
field :address do
column_width 50
end
end
edit do
field :name do
required true
help "Por favor llena este campo."
end
#field :key do
# required true
# help "Por favor llena este campo."
#end
field :company_name do
required true
help "Por favor llena este campo."
end
field :category do
required true
help "Por favor llena este campo."
end
field :winner do
default_value = false
help "Llenar sólo si este equipo es ganador del concurso nacional."
end
field :address do
required true
help "Por favor llena este campo."
end
field :team_members
field :events do
required true
help "Por favor llena este campo."
end
field :date_of_registration do
required true
help "Por favor llena este campo."
end
end
end
private
def generate_key
standing = "R"
self.events.each do |current_event|
if current_event.event_type == "Nacional"
standing = "N"
end
end
if self.winner == true
standing = "G"
end
region_key = self.events.last.region.key
year = self.date_of_registration.year
id = self.id.to_s.rjust(3, '0')
category = self.category.key
special = 0
case self.category.key
when "S1"
special = 1
when "S2"
special = 2
when "S3"
special = 3
when "S4"
special = 4
end
self.key = "#{standing}#{region_key}#{year}#{id}#{category}#{special}".upcase
end
end
This is what the logs on Heroku look like when I try to create a new Region: 当我尝试创建一个新区域时,这就是Heroku上的日志:
2015-06-02T21:05:45.566768+00:00 heroku[router]: at=info method=GET path="/admin/region?_pjax=%5Bdata-pjax-container%5D" host=sistema-de-evaluaciones-amte.herokuapp.com request_id=887af50d-bfb8-4a96-889d-e858d980838e fwd="189.241.62.189" dyno=web.1 connect=1ms service=415ms status=200 bytes=11925
2015-06-02T21:05:45.512300+00:00 app[web.1]: Rendered vendor/bundle/ruby/2.2.0/gems/rails_admin-0.6.7/app/views/rails_admin/main/index.html.haml within layouts/rails_admin/pjax (296.2ms)
2015-06-02T21:05:45.548529+00:00 app[web.1]: Completed 200 OK in 360ms (Views: 321.1ms | ActiveRecord: 22.2ms)
2015-06-02T21:05:47.282570+00:00 heroku[router]: at=info method=GET path="/admin/region/new?_pjax=%5Bdata-pjax-container%5D" host=sistema-de-evaluaciones-amte.herokuapp.com request_id=d8566b9a-943a-4265-afd2-6f339c52c633 fwd="189.241.62.189" dyno=web.1 connect=2ms service=59ms status=200 bytes=3960
2015-06-02T21:05:47.224731+00:00 app[web.1]: Processing by RailsAdmin::MainController#new as HTML
2015-06-02T21:05:47.260190+00:00 app[web.1]: Rendered vendor/bundle/ruby/2.2.0/gems/rails_admin-0.6.7/app/views/rails_admin/main/_submit_buttons.html.haml (12.7ms)
2015-06-02T21:05:47.224784+00:00 app[web.1]: Parameters: {"_pjax"=>"[data-pjax-container]", "model_name"=>"region"}
2015-06-02T21:05:47.272787+00:00 app[web.1]: Completed 200 OK in 48ms (Views: 34.2ms | ActiveRecord: 3.7ms)
2015-06-02T21:05:47.245920+00:00 app[web.1]: Rendered vendor/bundle/ruby/2.2.0/gems/rails_admin-0.6.7/app/views/rails_admin/main/_form_field.html.haml (1.3ms)
2015-06-02T21:05:47.222221+00:00 app[web.1]: Started GET "/admin/region/new?_pjax=%5Bdata-pjax-container%5D" for 189.241.62.189 at 2015-06-02 21:05:47 +0000
2015-06-02T21:05:47.260810+00:00 app[web.1]: Rendered vendor/bundle/ruby/2.2.0/gems/rails_admin-0.6.7/app/views/rails_admin/main/new.html.haml within layouts/rails_admin/pjax (21.0ms)
2015-06-02T21:05:50.197718+00:00 app[web.1]: Started POST "/admin/region/new" for 189.241.62.189 at 2015-06-02 21:05:50 +0000
2015-06-02T21:05:50.336844+00:00 app[web.1]: Processing by RailsAdmin::MainController#index as HTML
2015-06-02T21:05:50.336854+00:00 app[web.1]: Parameters: {"model_name"=>"region"}
2015-06-02T21:05:50.447221+00:00 app[web.1]: Rendered vendor/bundle/ruby/2.2.0/gems/rails_admin-0.6.7/app/views/rails_admin/main/index.html.haml within layouts/rails_admin/application (96.7ms)
2015-06-02T21:05:50.199887+00:00 app[web.1]: Processing by RailsAdmin::MainController#new as HTML
2015-06-02T21:05:50.199969+00:00 app[web.1]: Parameters: {"utf8"=>"✓", "authenticity_token"=>"jajkJUlQhU2i1z0io6pmDe/NZkzB1kA2hGWL8dYauCb22TYFftstrNop57hKTF2Ye0DxS1SNbZt/WQGoHWn+6w==", "region"=>{"name"=>"Prueba"}, "return_to"=>"https://sistema-de-evaluaciones-amte.herokuapp.com/admin/region", "_save"=>"", "model_name"=>"region"}
2015-06-02T21:05:50.224054+00:00 app[web.1]: Redirected to https://sistema-de-evaluaciones-amte.herokuapp.com/admin/region
2015-06-02T21:05:50.224993+00:00 app[web.1]: Completed 302 Found in 24ms (ActiveRecord: 10.2ms)
2015-06-02T21:05:50.334556+00:00 app[web.1]: Started GET "/admin/region" for 189.241.62.189 at 2015-06-02 21:05:50 +0000
2015-06-02T21:05:50.240345+00:00 heroku[router]: at=info method=POST path="/admin/region/new" host=sistema-de-evaluaciones-amte.herokuapp.com request_id=346211ad-fcf1-4012-9239-3884d01ff0a3 fwd="189.241.62.189" dyno=web.1 connect=1ms service=42ms status=302 bytes=1218
2015-06-02T21:05:50.454815+00:00 app[web.1]: Rendered vendor/bundle/ruby/2.2.0/gems/rails_admin-0.6.7/app/views/layouts/rails_admin/_navigation.html.haml (5.5ms)
2015-06-02T21:05:50.454724+00:00 app[web.1]: Rendered vendor/bundle/ruby/2.2.0/gems/rails_admin-0.6.7/app/views/layouts/rails_admin/_secondary_navigation.html.haml (4.9ms)
2015-06-02T21:05:50.485504+00:00 app[web.1]: Rendered vendor/bundle/ruby/2.2.0/gems/rails_admin-0.6.7/app/views/layouts/rails_admin/pjax.html.haml (10.2ms)
2015-06-02T21:05:50.486076+00:00 app[web.1]: Completed 200 OK in 149ms (Views: 114.7ms | ActiveRecord: 24.4ms)
2015-06-02T21:05:50.502507+00:00 heroku[router]: at=info method=GET path="/admin/region" host=sistema-de-evaluaciones-amte.herokuapp.com request_id=e2439dad-9f8d-407b-b3c8-5229bad08b58 fwd="189.241.62.189" dyno=web.1 connect=6ms service=167ms status=200 bytes=16948
And this is what I see when I do the same in my local server, as you can see, in the POST block it is clear that the "key" element is an argument for the insert query. 这就是我在本地服务器中执行相同操作时所看到的,正如您所看到的,在POST块中,很明显“key”元素是插入查询的参数。
Started POST "/admin/region/new" for 127.0.0.1 at 2015-06-02 16:10:18 -0500
Processing by RailsAdmin::MainController#new as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"r8fQgZOsomw4C3P/aL/byEqEIY+BnBAm0WpCdNzYPf+n0v1yENTdUlXE/AfT5JBValjfKVmFubfKIQipxSX1qw==", "region"=>{"name"=>"Prueba"}, "return_to"=>"http://localhost:3000/admin/region", "_save"=>"", "model_name"=>"region"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Role Load (0.2ms) SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = ? AND (((roles.name = 'super_admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["user_id", 1]]
Role Load (0.3ms) SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = ? ORDER BY "roles"."id" ASC LIMIT 1 [["user_id", 1]]
(0.3ms) begin transaction
SQL (0.6ms) INSERT INTO "regions" ("name", "created_at", "updated_at", "key") VALUES (?, ?, ?, ?) [["name", "Prueba"], ["created_at", "2015-06-02 21:10:18.391401"], ["updated_at", "2015-06-02 21:10:18.391401"], ["key", "Pr"]]
(150.7ms) commit transaction
Redirected to http://localhost:3000/admin/region
Completed 302 Found in 175ms (ActiveRecord: 152.2ms)
I found the error! 我发现了错误! It had to do with an SQL trigger someone else on the team directly created on the PostgreSQL database on Heroku on an earlier stage of development because it was being called at the same time as the callback, once I dropped it everything started to work as intended. 它与SQL引发器上的其他人直接在Heroku上的PostgreSQL数据库上创建的早期开发阶段有关,因为它在回调的同时被调用,一旦我放弃它,一切都开始按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.