I'm fairly new to Rails, so this will likely end up being something obvious; however I've just spent the better part of a day pulling my hair out over this issue.
I have a rails app which I've been working on for awhile, however I only started implementing mailers today. I followed the Rails ActionMailer tutorial here: http://guides.rubyonrails.org/v3.0.3/action_mailer_basics.html and the mailer works fine in a new example app. However, when I repeated those steps verbatim in my existing rails app (running in Development environment) I receive the below error. It creates the entry in the DB, correctly sends both the plain text & HTML emails and THEN generates the error. All I'm trying to do here is send a welcome email upon the creation of a new account, but I'm getting the same error when I try to send any email from any controller.
The specific error I'm seeing after it sends the welcome email is:
Completed 500 Internal Server Error in 280ms
NoMethodError (undefined method `error' for true:TrueClass):
app/controllers/musers_controller.rb:52:in `block in create'
app/controllers/musers_controller.rb:50:in `create'
Note that to not mess up my existing User table, I created a temporary scaffold & mailer called Muser which I plan on deleting once I'm confident this will work correctly on my user table.
Code
Error in log:
Started POST "/musers" for 127.0.0.1 at 2013-07-10 20:32:34 -0400
Processing by MusersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"OuoEmsjkAVBHZwqPO5b/O4eKw6iZBaLP6vUT6f9WCOI=", "muser"=>{"name"=>"New User", "email"=>"User#email.com"}, "commit"=>"Create"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
(0.1ms) begin transaction
SQL (0.6ms) INSERT INTO "musers" ("created_at", "email", "name", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Thu, 11 Jul 2013 00:32:34 UTC +00:00], ["email", "User#email.com"], ["name", "New User"], ["updated_at", Thu, 11 Jul 2013 00:32:34 UTC +00:00]]
(1.7ms) commit transaction
Rendered muser_mailer/registration_confirmation.html.erb (0.1ms)
Rendered muser_mailer/registration_confirmation.text.erb (0.0ms)
Completed 500 Internal Server Error in 280ms
NoMethodError (undefined method `error' for true:TrueClass):
app/controllers/musers_controller.rb:52:in `block in create'
app/controllers/musers_controller.rb:50:in `create'
Rendered /usr/local/rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.6ms)
Rendered /usr/local/rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (2.5ms)
Rendered /usr/local/rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (15.3ms)
--I should note that lines 50 & 52 of the musers_controller (where this error is being generated) correspond to the 'respond_to do' & 'MuserMailer.... .deliver' lines in the controller code below.--
The controller action:
# POST /musers
# POST /musers.json
def create
#muser = Muser.new(params[:muser])
respond_to do |format|
if #muser.save
MuserMailer.registration_confirmation(#muser).deliver
format.html { redirect_to #muser, notice: 'Muser was successfully created.' }
format.json { render json: #muser, status: :created, location: #muser }
else
format.html { render action: "new" }
format.json { render json: #muser.errors, status: :unprocessable_entity }
end
end
end
Mailer:
class MuserMailer < ActionMailer::Base
default from: "EmailAddress#Inter.net"
def registration_confirmation(muser)
#muser = muser
mail(:to => muser.email, :subject => "Registered")
end
end
I don't think that the issue is with my smtp, mail setup, or variables since it does actually add to the DB & send the emails correctly. If I comment out the line in the controller which calls the mail action the error disappears, so I don't think the problem is with my muser routes. This undefined method 'error' for true:TrueClass is driving me nuts. I did recently install Devise on my Users table, so I don't know if that could be causing the issue?
For lack of a better term, it feels like the issue is with how Rails wants to route after sending the emails; as if I need to put a Return or specify a route at the end of my mailer action telling the server to head back to the controller action. In other words, I'm lost!
Update
Below are the two mailer view files I'm using.
registration_confirmation.html.erb
<h3><%= #muser.name %>! You sweet sweet fool!</h3>
<p>Thank you for registering!</p>
registration_confirmation.text.erb
Thank you for registering!
Update 2
Here's my model for Muser:
class Muser < ActiveRecord::Base
attr_accessible :email, :name
end
I solved this issue - there was the errant line config.action_mailer.logger = true in my config/environments/development.rb file that was causing the issues. Once removed everything worked perfectly.
Related
IMPORTANT NOTE: The id does not matter, it is same in all cases.
I am only seeing the name field, not description, hours, etc even though these aren't null.
I have declared all standard routes through resources (with default format json), not individually.
I have even tried creating a projects/show.json.jbuilder file:
json.name #project.name
json.description #project.description
json.hours #project.hours
json.ownername #project.ownername
My projects/show method:
#project = Project.find(params[:id])
render :json => #project
FIRST EDIT:
I added logger.debug right before defining #project in my show method.
Now in my command prompt window for the local server, I am seeing:
Started GET "/projects/12" for 127.0.0.1 at 2015-02-25 16:36:45 -0500
ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by ProjectsController#show as HTML
Parameters: {"id"=>"12"}
Project Load (0.1ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" = ? LIMIT 1 [["id", 12]]
#<Project:0x007f95477e72f8>
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 51]]
Completed 200 OK in 42ms (Views: 15.2ms | ActiveRecord: 0.9ms)
I am wondering why I am seeing the "User Load" part, since my Project model does not belong to a User object or have any relationship with it (though in the past it may have had before that relationship was removed). Also, I don't think I saw
#<Project:0x007f95477e72f8>
earlier.
I solved my problem this way.
Basically, projects/12.json currently works and not projects/12. Since I am more interested in obtaining the data client side, it is ok for me. I would need a projects/show.html.erb page that calls all the project data through view helper methods, for the HTML to work.
def show
#project = Project.find(params[:id])
respond_to do |format|
format.html
format.json
end
end
projects/12.json works because I have a projects/show.json.jbuilder file (as I shared in my original question post):
json.name #project.name
json.description #project.description
json.hours #project.hours
json.ownername #project.ownername
Whenever I have a user that has not confirmed their account, if I click on "Resend Confirmation Instructions" after putting in the correct email address, an email gets successfully sent (in development anyway) and then I get this error message:
ActionController::UnknownFormat at /users/confirmation.user
ActionController::UnknownFormat
At this URL:
http://localhost:3000/users/confirmation.user
Here is my ConfirmationsController
class ConfirmationsController < Devise::ConfirmationsController
private
def after_confirmation_path_for(resource_name, resource)
new_session_path(resource_name)
end
end
Here is the log for the entire event:
Started POST "/users/confirmation.user" for 127.0.0.1 at 2014-12-28 19:23:23 -0500
Processing by ConfirmationsController#create as
Parameters: {"utf8"=>"✓", "authenticity_token"=>"SaKoS0Xo3PEkPLDRGt0s1dDsFL3sWIGAS6TZ69bhF4E=", "user"=>{"email"=>"xyz#test.com"}, "commit"=>"Resend confirmation instructions"}
User Load (3.6ms) SELECT "users".* FROM "users" WHERE "users"."email" = 'xyz#test.com' ORDER BY "users"."id" ASC LIMIT 1
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."confirmation_token" = '03f1df6d19e7ca7f5b3f0789e159ba3b31b24895fffc6124def25467e901b5df' ORDER BY "users"."id" ASC LIMIT 1
(1.5ms) BEGIN
SQL (14.2ms) UPDATE "users" SET "confirmation_sent_at" = $1, "confirmation_token" = $2, "updated_at" = $3 WHERE "users"."id" = 4 [["confirmation_sent_at", "2014-12-29 00:23:23.286698"], ["confirmation_token", "03f1df6d19e7ca7f5b3f0789e159ba3b31b24895fffc6124def25467e901b5df"], ["updated_at", "2014-12-29 00:23:23.296387"]]
(1.5ms) COMMIT
Rendered devise/mailer/confirmation_instructions.html.erb (0.8ms)
Devise::Mailer#confirmation_instructions: processed outbound mail in 20.1ms
Sent mail to xyz#test.com (39.6ms)
Date: Sun, 28 Dec 2014 19:23:23 -0500
From: noreply#myapp
Reply-To: noreply#myapp
To: xyz#test.com
Message-ID: <54a09efb589ec_12f63fc655865be02817a#myputer.local.mail>
Subject: Welcome to My App! Please confirm your email address
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<center><img alt="Logo" height="50" src="http://someurl.com/assets/logo.png" width="153" /></center>
<p>Welcome to My App, xyz#test.com! Please confirm your email address by clicking the link below:</p>
<p>Confirm my account</p>
<p>Once you confirm, you are welcomed to join the community by sharing, reading and commenting on the breaking news that matters to you.</p>
<p>Best regards,
My App</p>
Completed 406 Not Acceptable in 161ms
ActionController::UnknownFormat - ActionController::UnknownFormat:
What could be causing this?
Edit 1
Please note that this error is NOT the same as other errors. In this case, the email IS successfully sent. It is just the page/action that it is redirected to that generates the error. THAT is the issue! The issue IS NOT with what happens AFTER you press the link in the confirmations email that is sent to the user. It is what happens to the Rails app AFTER the confirmation email is successfully sent. That is why I pasted the ConfirmationsController at the top of the question, because I suspect it is central to this issue.
For re-sending the confirmation instructions (Resend Confirmation Instructions), what is the helper method you have used in your code?
It can be resolved in either of 2 solutions mentioned below:
Solution 1:
If you have used a similar one like below, please remove the parameter you have passed through it.
As per the issue reported on Github (https://github.com/plataformatec/devise/issues/2834):
In your confirmation email view you're probably passing a parameter to your url helper, something like this:
user_confirmations_url(#some_variable)
That url does not require an argument and because you are sending one, it is being set as the url format instead of replacing a url parameter.
Solution 2:
Refer this link
Hope that helps :)
The task model has just one field : title.
I've made a form to add a new task with one single field : title
But in the create method, we can see that title is filled by "test"
but in the query, we can see "nil" ... any ideas ?
thanks
Started POST "/tasks" for 127.0.0.1 at 2013-01-03 13:16:44 -0500
Processing by TasksController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"iWaK1QX6VCyeUCueLrRNErJEtdm/ZNxg4d3LU0vKjnY=", "task"=>{"title"
=>"test"}, "commit"=>"Add a new task "}
(0.1ms) begin transaction
SQL (0.9ms) INSERT INTO "tasks" ("created_at", "title", "updated_at") VALUES (?, ?, ?) [["created_at", Thu, 03 Jan 2013 18:16:44 UTC +00:00], ["title", nil], ["updated_at", Thu, 03 Jan 2013 18:16:44 UTC +00:00]]
(0.8ms) commit transaction
Redirected to http://0.0.0.0:3000/tasks
Completed 302 Found in 8ms (ActiveRecord: 1.8ms)
here is the create method
def create
#task = Task.new(params[:post])
if #task.save
redirect_to tasks_path, :notice => "Task successfully saved"
else
render "new"
end
end
The problem is that you are fetching post instead of task
#task = Task.new(params[:task])
Make sure your attribute is accessible or you won't be able to mass-assign changes to it:
class Task < ActiveRecord::Base
attr_accessible :title
end
You should have unit tests that properly exercise your models to be sure that they can be updated as you do in the controller. Those will quickly uncover any attributes which have not been correctly flagged.
Rails 2.3 and prior were not strict about this, you could mass-assign anything, but Rails 3 will not assign these attributes unless they are specifically allowed.
Make sure
attr_accessible :title
is in your Task model (task.rb)
UPDATE:
change params[:post] to params[:task]:
#task = Task.new(params[:task])
In your tasks_controller.rb , you must have create method which will handle POST request and accept parameters which are passed though request .
def create
task = Task.new(params[:task])
task.save
end
I'm trying to create user-generated posts. I know that the posts are being created in the
db, but are not displaying. Terminal puts:
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
SQL (18.4ms) INSERT INTO "events" ("content", "created_at", "updated_at",
"user_id") VALUES (?, ?, ?, ?) [["content", "Test post."], ["created_at",
Sat, 15 Oct 2011 06:36:49 UTC +00:00], ["updated_at",
Sat, 15 Oct 2011 06:36:49 UTC +00:00], ["user_id", 1]]
Redirected to http://localhost:3000/events
Started GET "/events" for 127.0.0.1 at Sat Oct 15 00:36:49 -0600 2011
Processing by EventsController#show as HTML
Completed 404 Not Found in 1ms
ActiveRecord::RecordNotFound (Couldn't find Event without an ID):
app/controllers/events_controller.rb:22:in `show'
Saying the same thing, but my app gives me the same error:
Couldn't find Event without an ID
app/controllers/events_controller.rb:22:in `show'
Is this a problem with my Events_Controller method "show":
def show
#title = "Your Events"
#event = Event.find(params[:id])
end
Or a routine issue? I'm trying to display an index of all events created.
Thanks in advance for any help.
In EventsController on line 13 you have:
redirect_to events_path
I believe this corresponds to the sixth line in your log above ("Redirected to http://localhost:3000/events").
When you use redirect_to, however, it initiates a new GET request, and as you did not specify any parameters params is consequently empty. That's why params[:id] is nil and Event.find(params[:id]) throws the error you're seeing.
Instead of using redirect_to, are you sure you shouldn't be using render :action => :show or render :action => :index? Unlike redirect_to, render does not initiate a new request, it merely renders the specified view, but within the current context (where, in your case, #event is already defined.
For more information on render vs. redirect_to read the Rails Guide on Layouts and Rendering, section 2 in particular.
Ok weird error. Everything was working fine.. and now its not.
Currently I have a simple many to one association.
Route is set up like this:
resources :apps do
resources :forms
end
App:
has_many :forms
Form:
belongs_to :app
Forms_controller index action:
def index
#app = App.find(params[:app_id])
#forms = #app.forms
respond_to do |format|
format.html # index.html.erb
format.json { render json: #forms }
end
end
I've taken every bit of code/html out of the forms.html.erb layout file so it should be rendering a blank page.
Instead I'm getting this error:
undefined method `status' for nil:NilClass
status isn't even defined anywhere in my app
help would be greatly appreciated.
EDIT:
Here is what is displayed in my development.log file
Started GET "/apps/4/forms" for 127.0.0.1 at 2011-09-05 23:14:16 -0700
Processing by FormsController#index as HTML
Parameters: {"app_id"=>"4"}
[1m[36mApp Load (0.1ms)[0m [1mSELECT "apps".* FROM "apps" WHERE "apps"."id" = ? LIMIT 1[0m [["id", "4"]]
[1m[35m (0.1ms)[0m SELECT COUNT(*) FROM "forms" WHERE "forms"."app_id" = 4
0
[1m[36mForm Load (0.1ms)[0m [1mSELECT "forms".* FROM "forms" WHERE "forms"."app_id" = 4[0m
Rendered forms/index.html.erb within layouts/forms (1.2ms)
Completed 500 Internal Server Error in 37ms
NoMethodError (undefined method `status' for nil:NilClass):
I had a similar issue - I had a method named 'response' that something internal to Rails was calling 'status' on and it similarly bailed without a stack trace to speak of.
With things named 'app' and 'forms' you might be running into something similar.
#app was not found, you can fetch forms within try
def index
#app = App.find(params[:app_id])
#forms = #app.try(:forms)
respond_to do |format|
format.html # index.html.erb
format.json { render json: #forms }
end
end
If your template handles the #app, and it's important to have it, better to handle the exception:
def index
#app = App.find!(params[:app_id]) # raise an exception until find
#forms = #app.forms
rescue
flash[:error] = "App not found!"
end
First, please check log/development.log or on browser. It should help you where the error hapened.
Next, what is params value? Check log/development.log. It may look like the followings:
...
Processing FormsController#index (for 127.0.1.1 at YYYY-MM-DD hh:mm:ss) [GET]
Parameters: {...}
...
For internal server errors your application could not be started properly. You should check your server error log. It may give you insight as to what the problem is. If you, or a team member as the case may be, didn't make any changes that broke the application then you should check with your host. Perhaps they made changes to your environment that are causing an error.