Create issue on production but not on localhost - ruby-on-rails

On my rails application, I am receiving an error on production(heroku) but not on my localhost. All of my migrations are done and I can add it through the console, but I cannot get this to work in the controller.
So the area where the error is occuring is here:
respond_to do |format|
if #registration.save
EventMailer.new_registration_notification(#registration, #current_event).deliver
Recipient.create :first_name => #registration.first_name, :last_name => #registration.last_name, :email => #registration.email, :netID => #registration.netID, :event_id => #registration.event_id, :mailing_id => nil
format.html { redirect_to event_path(#current_event), notice: 'Registration was successful' }
Again, I am not getting an error in my development local environment but I am in production. They are both using Postgres. So my registrations ARE saving but Recipient is never created and I get an error. I've checked the heroku logs by running:
herkou logs
but there are no errors shown. On the front end, you see an error that says "Something went wrong. If you're the application owner please check your logs".

Fixed my problem, the problem was with sending the mailer. I did not notice that the mailer wasn't sending and just assumed it was an issue creating the Recipient. I needed to fix some configurations in the production.rb file to use the SMTP server. After fixing that the issue went away

Related

NoMethodError: undefined method `const_defined?' when referring to class methods

I am building an email sending system into an already fairly complex rails app.
From a controller I'm trying to call a method that builds an email and then delivers it.
In controller:
response_for :create, :update do
estimate = current_object.estimate.reload
flash[:success] = "Tier Save Successful. "
flash[:success] += send_estimate_email if estimate.verify && estimate.tiers.select{|t| !t.verified?}.empty?
flash[:notice] = "This service bundle was changed for a previously verified service bundle. Please re-verify after changes are complete" if current_object.auto_unverified
redirect_to [current_object.estimate]
end
def send_estimate_email
return "" if current_object.estimate.requestor_email.blank? || !current_object.estimate.requestor_notified_at.blank?
begin
estimate = current_object.estimate
host = request.host_with_port
binding.pry
success = EstimateEmailer.deliver_verification_complete(estimate, host)
current_object.estimate.notes.create!(:user_id => User.find(:first, :conditions => {:user_type_id => UserType::ROOT.id}).id, :title => "Verification Complete Email Sent", :body => "TO: #{success.to}\nSUBJECT: #{success.subject}\nBODY: #{success.body}")
rescue Exception => e
logger.warn(e)
success = false
end
if success
current_object.estimate.update_attribute(:requestor_notified_at, Time.now)
current_object.estimate.notes.create!(:title => "Auto-email sent to requestor", :body => "TO: #{success.to}\nSUBJECT: #{success.subject}\nBODY: #{success.body}", :user_id => current_user.id)
return "Automatic estimate email was successfully sent to #{current_object.estimate.requestor_email}"
else
return "However, automatic estimate email could not be sent due to an error"
end
end
I don't think there's anything wrong with the email code itself, but for some reason I get this error on the EstimateEmailer.deliver_verification_complete(estimate, host) call:
NoMethodError: undefined method `const_defined?' for "estimate":String
setting a breakpoint in the controller before the EstimateEmailer call I notice that I get the error whenever I try to refer to other classes. Just calling EstimateEmailer or any other class name returns the same error.
I've run into issues with undefined method const_defined? in the past. I'm guessing the issue isn't specific to emailing, but I haven't the slightest idea where to begin debugging.
def verification_complete(estimate, host)
request_id = estimate.integration_transactions.empty? ? "Internal ID #{estimate.id}" : estimate.integration_transactions.last.source_id
#estimate = estimate
#estimate_url = public_estimate_url(estimate.public_id, :host => host, :protocol => 'https')
mail(
subject: "Ref ##{estimate.project_code} Application Hosting Estimate Available for Review: #{request_id}",
to: [estimate.requestor_email, estimate.project_manager_email].reject{|email| email.blank? }.join(','),
from: SUPPORT_ADDRESS
)
end
You're using make_resourceful which has an incompatibility with current versions of ActiveSupport. I've just lodged a PR that addresses it: https://github.com/hcatlin/make_resourceful/pull/16.
Alternatively, you can point to the remote branch in your Gemfile:
gem 'make_resourceful', github: 'carpodaster/make_resourceful', branch: 'fix/incompatibility-with-active-support-dependencies'
I assume this problem only occurs in your development environment, right? In that case, you should be able to avoid it by setting config.eager_load = true in config/environments/development.rb.

Devise Password Reset Issue Rails 4

I'm using Devise for authentication with a Rails 4 app and am having issues with the password reset. Locally, everything works fine, when I paste the reset link in (i.e. localhost:3000/users/password/edit?reset_password_token=e_f3ZpqrE_rTBZmKJk_E) it works as expected.
On Heroku however, Devise seems to not even notice the :reset_password_token param, and automatically redirect to /users/signin with the notice "You can't access this page without coming from a password reset email. If you do come from a password reset email, please make sure you used the full URL provided."
Here's is an example of the link that is being generated: http://mysite.io/users/password/edit?reset_password_token=anzYNreZEcz4-dtZy5Uf
I even overrode the assert_reset_token_passed method in my own controller to check if params[:reset_password_token] was actually blank, and for some reason it is, rails is not pulling this out of the url. Here's my modified method:
def assert_reset_token_passed
logger.info params[:reset_password_token] #This is blank somehow
if params[:reset_password_token].blank?
set_flash_message(:alert, :no_token)
redirect_to new_session_path(resource_name) #This is where the redirect happens
end
end
Any help would be much appreciated.
I was having the exact same issue. The fix for me was to update the config.action_mailer.default_url_options in production.rb to include the full host (in my case 'www.mydomain.com' vs 'mydomain.com').
To clarify, it used to be
config.action_mailer.default_url_options = { :host => 'mydomain.com' }
and now it's
config.action_mailer.default_url_options = { :host => 'www.mydomain.com' }

Analytics API integration rails failing on heroku production

I am currently developing a ruby on rails application which includes the gattica gem to fetch Google Analytics data. When I fetch my data:
https://github.com/activenetwork/gattica
gs = Gattica.new({:email => 'johndoe#google.com', :password => 'password', :profile_id => 123456})
results = gs.get({ :start_date => '2008-01-01',
:end_date => '2008-02-01',
:dimensions => 'browser',
:metrics => 'pageviews',
:sort => '-pageviews'})
on development I will simply receive a response which I can parse to my application.
However on production the page returns a 500 error and in my Gmail inbox I receive a message about a suspicious login being caught.
Is there any way I can fix this issue?
PS: my application is hosted on Heroku.
With kind regards,
Dennis
You're getting the 500 error because Google is blocking your heroku ip from accessing your account. They aren't sure it's you.
You need to change your activity settings to authorize that ip/domain.
Read this: https://support.google.com/accounts/answer/1144110?hl=en&ref_topic=2401957
Also, its a good idea to read your logs when debugging these kinds of errors. Rails.logger.debug results could shed some light.

Unable to catch Mysql2::Error in Rails

Currently testing the following code:
def db_check
begin
schema_call = ActiveRecord::Base.establish_connection(
:adapter => 'mysql2',
:host => 'localhost',
:database => 'dev_db',
:username => 'dev_user',
:password => 'dev_pw').connection.execute("SELECT * FROM schema_migrations LIMIT 1")
if schema_call
render :status => 200, :file => "public/success.html"
else
render :status => 500, :file => "public/query_fail.html"
end
rescue Exception => e
puts "#{e.class} ;; #{e.message}"
logger.debug "#{e.class}"
render :status => 500, :file => "public/500.html"
end
end
The eventual goal is to have a call to a MySQL server to see if 1) the server is still up and 2) if the database is available. If the connection doesn't work, an Error is thrown, so I put the code in a rescue block. Unfortunately, even when I use rescue Exception, which I understand to be advised against, I still get a Mysql2::Error message in the browser (I also tried rescue Mysql2:Error, which had no effect).
The duplication of error logging in the rescue is extra attempts to get additional information to work with, but nothing has worked so far. Anyone know how to catch this error?
UPDATE: also, for additional context, testing the code with MySQL not running currently (condition if the DB server is down), get back the following:
Mysql2::Error
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
which makes partial sense, given the server is off, but I still don't understand why it isn't rescuing the error.
The reason the rescue isn't catching the Mysql2::Error is that the error isn't coming from the ActiveRecord::Base code, but rather is Rails throwing an error because it can't connect to the MySQL server that it expects (which I had turned off to test the above code).

Lost values after switching email sending from AR_Mailer to DelayedJob

I've been using AR_Mailer for about 6 months without ever running into problems. I recently added DelayedJob for some administrative background jobs. Since DelayedJob also handles emails very well (thanks to DelayedMailer gem) I completely removed AR_Mailer from my application.
Everything works perfectly except this email. The password that is generated automatically is now lost.
#app/models/notifier.rb
def activation_instructions(user)
from default_email
#bcc = BACK_UP
#subject = "Activation instructions"
recipients user.email
sent_on Time.now
body :root_url => root_url, :user => user
end
#app/views/notifier/activation_instructions.erb
Thanks for signing up.
Your password is <%=#user.password-%>. For security reasons please change this on your first connection.
[....]
Any idea on why this bug occurs?
Thanks!
Configuration: Rails 2.3.2 & DelayedJob 2.0.4
I found out where the problem was. I looked in the database at the entry created in the delayed_jobs table:
--- !ruby/struct:Delayed::PerformableMethod
object: LOAD;Notifier
method: :deliver_activation_instructions!
args:
- LOAD;User;589
The user parameter is reloaded from the database by delayed_job before sending the email. In that case, the password is lost because it's not stored in the database.
So I've updated the code in order to pass explicitly the password:
#app/models/notifier.rb
def activation_instructions(user, password)
from default_email
#bcc = BACK_UP
#subject = "Activation instructions"
recipients user.email
sent_on Time.now
body :root_url => root_url, :user => user, :password => password
end
#app/views/notifier/activation_instructions.erb
Thanks for signing up.
Your password is <%=#password-%>. For security reasons please change this on your first connection.
[....]
Hope this helps other too!

Resources