Heroku SocketError on POST submission - ruby-on-rails

When submitting a form to create a simple object, I'm getting a SocketError:
Completed 500 Internal Server Error in 526ms
SocketError (getaddrinfo: Name or service not known):
app/controllers/posts_controller.rb:58:in `block in create'
app/controllers/posts_controller.rb:57:in `create'
I've Google'd and searched through StackOverflow, everyone else with similar errors seems to be sending email. I'm not -- this is just a form to create a simple blog post that belongs to a user.
This app IS using SendGrid (though this particular functionality does not use it), however, even after removing SendGrid, the SocketError persists.
Here's the code from the controller, referenced in the error message:
# POST /posts
# POST /posts.json
def create
#user = User.find(params[:user_id])
#post = #user.posts.new(params[:post])
respond_to do |format|
if #post.save
format.html { redirect_to user_posts_path, notice: 'Post was successfully created.' }
format.json { render json: #post, status: :created, location: #post }
else
format.html { render action: "new" }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
Additionally, other posts mentioning this error seem to have issues with host declarations/URL's/URI's and weird configurations in production environment files. I don't seem to have these problems, and I can't seem to determine if this is a problem with my application or a host configuration issue with Heroku. I'm inclined to believe it's a mistake on my end.
I've been cranking on this for a while now, with no luck. Any help is appreciated!

Is a mail being delivered on post#save?
ActionMailer should be configured to use sendgrid manually on the Cedar stack:
https://devcenter.heroku.com/articles/sendgrid#actionmailer

Related

Heroku Application Error After Form Submittal, But No Error Logged?

I get an error when I try to create a new form with image uploading to Amazon S3.
I think its because of my photo uploads, but not too sure. when I do this in my development environment, no issues. I'm using Amazon S3, in development as well.
I checked heroku logs and I get no error.
Once I create a new form, it supposed to direct me to that show.html.erb page, with the id in the URL (ie: www.example.com/projects/this-is-new-page), but instead, it sent me to www.example.com/projects and the error.
Oh, I'm also using friendly_id gem
def create
#project = project.new(project_params)
respond_to do |format|
if #project.save
if params[:photos]
params[:photos].each { |image|
#project.project_images.create(photo: image)
}
end
format.html { redirect_to #project, notice: 'Trip was successfully created.' }
format.json { render :show, status: :created, location: #project }
else
format.html { render :new }
format.json { render json: #project.errors, status: :unprocessable_entity }
end
end
end
When I go back to www.example.com/projects/this-is-new-page, it works. But I only get this error right after I upload or submit a form.

Rails track which User emails have received an email to avoid sending duplicate emails

I'm working on creating a basic survey app to better learn Rails.
Right now I'm working on learning how to send emails. I've gotten it to work, but now I'm running into a scenario where I send out a lot of duplicate emails. The duplicates are coming out because I'm only checking for a status of Submitted on PUT and then sending out the email.
def update
p params
# unless signed_in?
# redirect_to signin_path
# end
respond_to do |format|
if #survey.update(survey_params)
#survey.check_email
if #survey.status == "Submitted"
SurveyMailer.survey_created(#survey).deliver
end
format.html { redirect_to #survey, notice: 'Survey was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #survey.errors, status: :unprocessable_entity }
end
end
end
What's the best way to check/track which emails for a survey I've sent it out to? Basically some sort of "if user has not been notified that a survey has been shared with them, send it out, otherwise don't"

Rails session store stopped working correctly

I have a Rails 3.2 app that uses session store in the controllers to get the user back to the screen they were previously on.
It's been working fine for over a year. All of a sudden, the production version on Heroku, has started having issues. The user is looking at a worequest and clicks on the following in order to add a comment.
<%= link_to 'New Comment', new_comment_path(:worequest_id => #worequest.id), :class => 'btn btn-primary' %>
This is the code I use:
def new
#comment = Comment.new
#comment.build_attachment
#worequest = params[:worequest_id]
session[:return_to] ||= request.referer
respond_to do |format|
format.html # new.html.erb
format.json { render json: #comment }
end
end
# POST /comments
# POST /comments.json
def create
#comment = Comment.new(params[:comment])
respond_to do |format|
if #comment.save
if session[:return_to] != nil
format.html { redirect_to session.delete(:return_to), notice: 'Comment was successfully created.' }
format.json { render json: #comment, comment: :created, location: #comment }
else
format.html { redirect_to :back, notice: 'Comment was successfully created.' }
format.json { render json: #comment, comment: :created, location: #comment }
end
else
format.html { render action: "new" }
format.json { render json: #comment.errors, status: :unprocessable_entity }
end
end
end
In development and staging (on Heroku), the user goes back to the worequest after entering a new comment.
Now in Production, the url looks like this:
mywebsite/comments instead of mywebsite/worequests/639
I'm not even sure where the session[:return_to] gets stored. Therefore, I'm having trouble debugging the issue.
Thanks for your help!!
Are you able to replicate this behavior 100% of the time, or just seeing it sometimes in your log?
It looks like someone is getting to comments#new from /comments (comments#index). Is there a route to /comments?
Run rake routes to see all your routes.
If there is a route to comments#index, and there's no reason for it to be exposed because you only intend for people to post comments from within the context of a specific article, consider removing it the comments#index route.
Also, one thing to consider, request.referrer is not always available. It's sent by the client who may choose not to send it (e.g. certain privacy extensions remove this header).

"No connection could be made because the target machine actively refused it" with Rails 3.2

I'm using Rails 3.2, and when I try using UserMailer to send an email, it gives me the error:
"Errno::ECONNREFUSED (No connection could be made because the target machine actively refused it. - connect(2))"
on this line:
UserMailer.welcome_email(#user).deliver
This is the create function the email is supposed to be delivered in:
def create
#user = User.new(params[:user])
respond_to do |format|
if #user.save
UserMailer.welcome_email(#user).deliver
format.html { redirect_to #user, notice: 'User was successfully created.' }
format.json { render json: #user, status: :created, location: #user }
else
format.html { render action: "new" }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
Does anyone know how to fix this problem? I'm pretty sure I've been able to send emails fine before.
Found the issue, it seems I forgot to add setup_mail.rb under config/initializers.

How to send data from Matlab to Rails

I'm very new to Rails and web development.
I'm generating a bunch of objects in Matlab and I'd like send these objects to a database in my Rails app. Can anyone advise me on how to do this?
So far, on the Rails end, I've generated basic scaffolding for my data. I can add objects to my database using a form at '/myobjects/new'.
On the Matlab end, I've been trying to add objects using HTTP POST requests, like so:
s = urlread('http://localhost:3000/myobjects.json','POST',{'myobject','{name1:''value1''}'})
This fails and prints the following to the Rails console:
Started POST "/myobjects.json" for 127.0.0.1 at 2012-06-16 11:48:28 -0400
Processing by MyobjectsController#create as JSON
Parameters: {"myobject"=>"{name1:'value1'}"}
WARNING: Can't verify CSRF token authenticity
Completed 500 Internal Server Error in 1ms
NoMethodError (undefined method `stringify_keys' for "{name1:'value1'}":String):
app/controllers/myobjects_controller.rb:43:in `new'
app/controllers/myobjects_controller.rb:43:in `create'
This approach might be way off base, but hopefully the code above makes my goal clear. Can anyone tell me how to fix my code, or suggest a better strategy for getting my data into rails?
EDIT
At the moment my new and create methods look like this (but I can change them as required)
# GET /irs/new
# GET /irs/new.json
def new
#ir = Ir.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #ir }
end
end
# POST /irs
# POST /irs.json
def create
#ir = Ir.new(params[:ir])
respond_to do |format|
if #ir.save
format.html { redirect_to #ir, notice: 'Ir was successfully created.' }
format.json { render json: #ir, status: :created, location: #ir }
else
format.html { render action: "new" }
format.json { render json: #ir.errors, status: :unprocessable_entity }
end
end
end
In the end I gave up trying to do this with matlab's built-in functions. Instead, I imported a Java library (Apache HttpComponents). Here's the script I came up with. This did the job.
javaaddpath(['utils/httpcomponents-client-4.2/lib/httpcore-4.2.jar']);
javaaddpath(['utils/httpcomponents-client-4.2/lib/httpclient-4.2.jar']);
import org.apache.http.impl.client.DefaultHttpClient
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity
httpclient = DefaultHttpClient();
httppost = HttpPost('http://127.0.0.1:3000/myobjects.json');
httppost.addHeader('Content-Type','application/json');
httppost.addHeader('Accept','application/json');
params = StringEntity('{"field1":"value1"}');
httppost.setEntity(params);
response = httpclient.execute(httppost);
You can avoid that specific problem by setting
class MyobjectsController < ApplicationController
protect_from_forgery :except => :create
...
end
within your controller. It disables the CSRF token validity check.

Resources