Sending mail from Rails works at console but not in my application...? - ruby-on-rails

I have a pretty simple Rails app and I want send an e-mail when a work request is created. I'm pretty new to Rails so I found some details on how to do it online. Set up a Mailer, configured it, etc. Set up my templates. Fine.
In my work_requests_controller.rb I have:
def create
#work_request = WorkRequest.new(params[:work_request])
respond_to do |format|
if #work_request.save
# Tell the mailer to send a welcome Email after save
PersonMailer.work_request_init_email(#work_request).deliver
format.html... etc.
In know mailing is working because if I go to the Rails console, create WorkRequest object, and use that exact same line (PersonMailer.work...) it sends the mail just fine. But when I create a work request in my application no mail is received, no error is displayed in the browser or in logs/development.log.
In the server output I see the HTML version was rendered, and I see the info about the e-mail and it all looks both hunky and dory. Since I get no errors I'm at a loss as to how to proceed.

OK, I am officially an idiot. :-)
Working on a different problem, I was editing application.rb. I figured I needed to restart the server to make it see those changes. Suddenly, the e-mails work from inside the app.
D'oh! I did not realize (rookie mistake) that I needed to restart the server for the app to see the e-mail configuration I had placed in environment.rb yesterday. I never tried that for some reason.
I see now that and other components are run only when the server starts up. So of course, when I started up console of course it runs all the initializers and so the e-mail configuration was visible to it and the e-mails were sent.
So the answer is, restart your server, stupid. Well, anyway, at least it's working now...I'll take it where I can get it!

I would start with moving your email command to your WorkRequest model as a 'after_create' action
after_create :send_init_email
def send_init_email
PersonMailer.deliver_work_request_init_email(self)
end
See if that works, or aleast gives you a better error msg.

Related

ActiveAdmin taking wrong http method for update and destroy actions

Rails version - 5.2
Active admin version - 2.9.0
I have installed and configured active admin in my rails API application. Everything is working fine, except for the update, delete action of any controller, and logout of the admin user.
Here is my applicaiton.rb file
I have added method override in application.rb file though it is taking the POST request method for any update or delete request. It is working fine in my local even though it is taking POST request but when I deployed the code on the staging environment. I have found this thing. On my staging environment, that route is not present hence it is giving 404 error.
Below is the screenshot of the Update admin user request.
Can someone please help me to fix this issue?
I have finally fixed the issue. I am assuming the issue might be with my staging web server configuration otherwise it was working fine in my local in both the environments local and staging.
Post the answer here so it might help people in future.
By default the browser only supports for GET and POST requests. If we want to use any other request methods then we need to pass that request method in the parameter _method. You can read more about it here.
That wasn't happening in my case though i have added config.middleware.use Rack::MethodOverride in application.rb.
For resolving the issue, I have added the use Rack::MethodOverride in my config.ru file. It means before running the rails application it will use this method. I have added this code and that's it everything is working fine now.

Created an entry in rails console, but app has no idea it exists

I entered
SiteData.create(site_name: "My Site", url: "http://mysite.com")
in the console, SiteData being one of my models. Now I can write SiteData.find(1) and it will echo back the entry. Great!
I'm using the db table to store all the info about my site, such as the name, the url, the Facebook page, the Google Plus pages, etc. SO I need to access it on every page. Okay, I'll use before_filter in the application controller to make this work, right?
So I went into my app, and in the Application controller, I wrote
before_filter :add_site_data
def add_site_data
#site_data = SiteData.find(1)
end
And I get an error: "Couldn't find SiteData with id=1"
What now????
This might not be the best way to do this. I am brand new to rails. But I'm totally stuck and have no idea what's going on or why here. I installed Postgres locally today and set up the test and dev dbs, but it seems to be working fine since I can still create/read/update/destroy entries from the console.
Please help! I'm going nuts here. I am using Rails 4.0.
Resetting the server fixed it.
If you know you're making entries in your database that aren't reflected when you run your app, reset the server.

Problem getting ar_mailer/ar_sendmail working on new server

Hey all. I've got a new app up and running on a new ubuntu server. It's working fine generally but i can't get ar_sendmail working. I'm following the instructions on this page:
http://www.ameravant.com/posts/sending-tons-of-emails-in-ruby-on-rails-with-ar_mailer
The setup is all done, ie i can "deliver mails" which just saves records in my Email table. Now i want to get the ar_sendmail daemon running to actually send them. (so i'm at 'Running ar_sendmail in daemon mode' in that web page).
First thing:
ar_sendmail --mailq
>>ar_sendmail: command not found
Ok...so, where is ar_sendmail? I have a look and there's an ar_sendmail file in the bin folder of the ar_mailer plugin, so i add the location of that to my path. I don't know if this was the right thing to do or not.
Ok, so try again.
ar_sendmail --mailq
/var/www/apps/millionaire/vendor/plugins/ar_mailer/bin/ar_sendmail:3:in `require': no such file to load -- action_mailer/ar_sendmail (LoadError)
from /var/www/apps/millionaire/vendor/plugins/ar_mailer/bin/ar_sendmail:3
hmm. Here's the offending file, there's not much there.
#!/usr/bin/env ruby
require 'action_mailer/ar_sendmail'
ActionMailer::ARSendmail.run
ok...so it literally is just trying to require this and can't find it. The file, action_mailer/ar_sendmail.rb is in the ar_mailer plugin, in it's lib folder. So, given that it's being called from inside the plugin, it should be able to see this right?
I've got a feeling that i'm way off the track here and have missed something simple. Can anyone set me straight? I'm using rails 2.3.4 in case that's relevant.
EDIT - i just realised something kind of dumb: when i call ar_sendmail from the command line like this, i'm just loading that one file, which doesn't know where it's supposed to look for the rest of the stuff, i think. Which really makes me think that i'm not trying to run the right thing. Is the ar_sendmail daemon a seperate program altogether, that i would get with apt_get or something?
EDIT2 - i made some progress by installing the ar_mailer gem (which the guide said i shouldn't do) and that does seem to run. It's sending some mail request somewhere and clearing the Email table of pending emails. Running ar_sendmail in -ov (oneshot verbal) mode i see it report this for example:
sent email 00000000019 from from#address.com to to#address.com: #
So, it actually looks like it's working now and i just need to set up the ACTUAL THING WHICH SENDS EMAILS. sigh. still grateful for any advice.
thanks, max
What you need is an smtp server. Checkout www.sendgrid.com.

Troubleshooting: Rails can't save to database in production?

I could use a little help trouble shooting this problem.
When using the app to create a new record nothing is being saved to the database.
There are no visible errors presented.
Dropping to the command line, and using the console with the same production environment, I can create a new object and save it (I have to bypass validations). If I look in mysql database I can see the record that I created from the console.
App works fine locally.
Any thoughts on what might be the problem?
Rails 2.0.2
Sounds like a validation error.
In your controller, try using save! (with the bang) to see if will throw a meaningful error.
I am not sure what code you have in the controller, but this might help show the problem
if my_object.save
log.debug 'object saved correctly'
else
log.debug my_object.errors.full_messages
end
Good luck, if this doesn't help. Try posting the relevant controller and model code.
Have you double checked that the request (with params, etc) works correctly in development?
If not, perhaps looking at the production log file will tell you where the request was routed (e.g. which controller and action, and with which parameters).

Not getting emails from ExceptionNotifier

I followed this tutorial on configuring the Rails plugin ExceptionNotifier.
I know that I have ActionMailer configured correctly because I am getting mail from other forms. I even have local_addresses.clear set so that it should be delivering mail no matter what.
I am using Apache with a mongrel_cluster running in the backend.
What am I missing?
You're using the SVN version of the plugin, which is probably unmaintained. Latest version can be found here.
Second thing which you can do is check the production log. Mailings get written to the log, so you'll see if Rails ever even tried to send it. If there are no entries, that means things are silently failing, which probably happens because -- for some reason -- exceptions are not caught properly.
If you added your ExceptionNotifier configuration information (your email address, etc.) into config/environment.rb, did you add it within the Rails::Initializer block or did you add it at the end of the file?
The tutorial you linked to doesn't specify where in the environment file to put the configuration information. The tutorial I followed (which might have been this one) does specify to put it outside the block.
Which things go inside that block and which outside is, frankly, still a little mysterious to me. But I thought this might answer your specific question.
Check your production log, exceptions can be throw in side the exception_notifier plugin, which prevent it from sending mails

Resources