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 :)
Related
Its been some time since I've worked with rails and I am jumping into a "project" for a company I work for..
Essentially what I am trying to do is redirect a user who logs in to the app to a specific page upon login..
For example an admin user will be redirected to an admin dashboard, an owner will be redirected to an owner dashboard and a driver to a driver dashboard..
Ive done this in the past using a single view and then filling it with elseif statements. But I found that to make my code look clunky and slow the app down.
I cant seem to find any docs (probably because I've been out of the game so long) on how to redirect to a specific url based on a user role.. Is this even possible? if so would someone be willing to share some resources as I am reaally struggling in this department.
Thanks in advance!
EDIT 1: Implemented ApplicationController method to find user role and redirect to appropriate page.
so I have added a few lines of code to my ApplicationController
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def after_sign_in_path_for(resource)
if current_user.role == "dispatch"
dashboard_dispatch_path
else
root_path
end
end
end
when I try to load the page, the url in the browser changes to the proper url, however I get the following error:
the stack trace from rails server:
Started POST "/users/sign_in" for 127.0.0.1 at 2018-04-01 12:10:58 -0600
Processing by Devise::SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"LJSAbgb3spFs0kEgFMYa40m2TZmZvH7weq53ciuGZAO07SUBgTHdxYTH0+MjRuYZIi+9++zIjnJP2rllVws5DA==", "user"=>{"email"=>"swixxxx#xxxxxltd.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Log in"}
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."email" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["email", "swixxxx#xxxxxxltd.com"], ["LIMIT", 1]]
(0.2ms) BEGIN
SQL (0.6ms) UPDATE "users" SET "current_sign_in_at" = $1, "sign_in_count" = $2, "updated_at" = $3 WHERE "users"."id" = $4 [["current_sign_in_at", "2018-04-01 18:10:58.835548"], ["sign_in_count", 2], ["updated_at", "2018-04-01 18:10:58.836544"], ["id", 2]]
(1.0ms) COMMIT
Redirected to http://localhost:3000/dashboard/dispatch
Completed 302 Found in 150ms (ActiveRecord: 2.6ms)
Started GET "/dashboard/dispatch" for 127.0.0.1 at 2018-04-01 12:10:58 -0600
ArgumentError - wrong number of arguments (given 3, expected 0):
app/controllers/dashboard_controller.rb:5:in `dispatch'
Started POST "/__better_errors/cda7f553a5c61c4b/variables" for 127.0.0.1 at 2018-04-01 12:10:58 -0600
EDIT 2: adds Controller Code:
class DashboardController < ApplicationController
def admin
end
def dispatch
end
def owner
end
def driver
end
def client
end
def guest
end
end
I'm trying to do the test signup at the end of lecture 113 in Upskill's Essential Web Developer course, however I get the following error and I have been over and over the previous lectures and cannot pinpoint where I have gone wrong.
Stripe::InvalidRequestError in Users::RegistrationsController#create
This customer has no attached payment source
Extracted source (around line #10):
8 def save_with_subscription
9 if valid?
10 customer = Stripe::Customer.create(description: email, plan: plan_id, card: stripe_card_token)
11 self.stripe_customer_token = customer.id
12 save!
13 end
Rails.root: /home/ubuntu/workspace/saasapp
Application Trace | Framework Trace | Full Trace
app/models/user.rb:10:in `save_with_subscription'
app/controllers/users/registrations_controller.rb:7:in `block in create'
app/controllers/users/registrations_controller.rb:3:in `create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"/4EiUCLerdc0o+vIbZWWZzxC3cm1TSjyWGs/lYq/H4RiT6zwohUJUQnZaIrxADF2RiWcs6G3BDXiRDQT/bEa4Q==",
"plan"=>"2",
"user"=>{"email"=>"test#example.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"},
"commit"=>"Sign up"}
Toggle session dump
Toggle env dump
Response
Headers:
None
LOG
Started POST "/users" for 81.140.28.63 at 2017-01-18 21:24:44 +0000
Cannot render console from 81.140.28.63! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by Users::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"GIGatf9j6proPQgjnjCwR6r6BBeDLCYa2lhOG453bQLwln1wTOEbVSPaPow6ZLGBcGpvX3Qm1Fw03vpQc0Xtsg==", "plan"=>"2", "user"=>{"email"=>"test#example.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
(0.1ms) begin transaction
User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = ? LIMIT ? [["email", "test#example.com"], ["LIMIT", 1]]
(0.2ms) rollback transaction
User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = ? LIMIT ? [["email", "test#example.com"], ["LIMIT", 1]]
Plan Load (0.1ms) SELECT "plans".* FROM "plans" WHERE "plans"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
Completed 500 Internal Server Error in 905ms (ActiveRecord: 0.9ms)
Stripe::InvalidRequestError (This customer has no attached payment source):
app/models/user.rb:10:in `save_with_subscription'
app/controllers/users/registrations_controller.rb:7:in `block in create'
app/controllers/users/registrations_controller.rb:3:in `create'
Rendering /usr/local/rvm/gems/ruby-2.3.0#saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
Rendering /usr/local/rvm/gems/ruby-2.3.0#saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
Rendered /usr/local/rvm/gems/ruby-2.3.0#saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (9.5ms)
Rendering /usr/local/rvm/gems/ruby-2.3.0#saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
Rendered /usr/local/rvm/gems/ruby-2.3.0#saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (4.3ms)
Rendering /usr/local/rvm/gems/ruby-2.3.0#saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
Rendered /usr/local/rvm/gems/ruby-2.3.0#saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (2.7ms)
Rendered /usr/local/rvm/gems/ruby-2.3.0#saasapp/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (43.3ms)
I would recommend dividing out the requests to see which part of it is actually failing. I suspect that the reason it's barfing is just because something else is happening and it's unable to manifest a meaningful error message.
Try this:
cus = Stripe::Customer.create(description: email)
self.stripe_customer_token = cus.id
cus.sources.create(source: stripe_card_token)
sub = Stripe::Subscription.create(customer: cus.id, plan: plan_id)
save!
I just hit the same issue.
I made the fix for the typo but still got the same error. It looks like the test card number used in the course video maybe another source for this
error too. I used a test number from the following page and all worked as expected.
https://stripe.com/docs/testing#cards
Just my 2cents!
Many thanks for taking the time to reply. So I gave it a few days and looked at it again last night and it turned out to be a rather simple and stupid mistake on my part.
In the lesson video and notes the instructor corrects a typo in the users.js file where he referenced an incorrect ID for the submit button in the form. He had typed
var submitBtn = $('#form-submit-btn');
but he had used the following in the form
var submitBtn = $('#form-signup-btn');
While I changed my users.js file to match the correction, somehow I had actually used #form-submit-btn' so when I corrected the users.js file per the lesson I actually caused the problem.
I'm using Rails 4 with Devise. Devise comes with existing forms to set the password so I wanted to add a link for the user to change their own password once logged in.
In my view, I have:
<li><%= link_to 'Change password', edit_user_password_path %></li>
This appears to render the right link:
Change password
When I try to visit this page, I get a flash message saying "You are already signed in", and the logs show:
Started GET "/users/password/edit" for ::1 at 2016-07-05 16:46:17 +1000
Processing by Devise::PasswordsController#edit as HTML
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Redirected to http://localhost:3000/
Filter chain halted as :require_no_authentication rendered or redirected
Completed 302 Found in 1ms (ActiveRecord: 0.1ms)
So I can see it hits the right controller and the right action, but then mysteriously redirects.
I don't really understand what this means but I would have thought that the page to edit your password would only work when signed in, so I'm not really sure why I'm getting an error about already being signed in.
Does anyone know Devise well enough to explain what's going on here?
edit_user_password_path is used for forgot password page, you need edit_user_registration_path.
Can you try to use:
edit_user_registration_path
I added a new migration to a table in my app and I migrate. Since it had errors, I dropped and migrated it. When I signed up for a user and tried to confirm it using email(devise,confirmable) it just won't confirm. It says invalid confirmation token. I have tried restarting the server. Dropping and migrating again, everything possible as far as I know. I am using Rails 3.2.9 and Ruby 1.9.3. Devise version is 3.1.0. Devise is also included in other gems I have added like rails-messaging and active-admin.
Started POST "/users/confirmation" for 127.0.0.1 at 2013-09-16 19:42:47 +0530
Processing by Devise::ConfirmationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"4lMxdlsMqRCJB1doxt/hTCQhUPvAoGPiSbr9wQA/ZAQ=", "user"=>{"email"=>"pro.aravind#gmail.com"}, "commit"=>"Resend confirmation instructions"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."unconfirmed_email" = 'pro.aravind#gmail.com' LIMIT 1
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."email" = 'pro.aravind#gmail.com' LIMIT 1
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."confirmation_token" = 'cc47150f51ec476aa40ea1d546e27c0dafc37ffc8bb82272a9f2377c863daed1' LIMIT 1
(0.1ms) begin transaction
(0.3ms) UPDATE "users" SET "confirmation_token"='cc47150f51ec476aa40ea1d546e27c0dafc37ffc8bb82272a9f2377c863daed1', "confirmation_sent_at" = '2013-09-16 14:12:47.174313', "updated_at" = '2013-09-16 14:12:47.175384' WHERE "users"."id" = 1
(175.3ms) commit transaction
Rendered devise/mailer/confirmation_instructions.html.erb (0.6ms)
Sent mail to pro.aravind#gmail.com (5099ms)
Date: Mon, 16 Sep 2013 19:42:47 +0530
From: please-change-me-at-config-initializers-devise#example.com
Reply-To: please-change-me-at-config-initializers-devise#example.com
To: pro.aravind#gmail.com
Message-ID: <523711df6bb56_3a029657f88282b#aravind-VPCEB46FGB.mail>
Subject: Confirmation instructions
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<p>Welcome pro.aravind#gmail.com!</p>
<p>You can confirm your account email through the link below:</p>
<p>Confirm my account</p>
Redirected to http://localhost:3000/users/sign_in
Completed 302 Found in 5387ms (ActiveRecord: 0.0ms)
Going to add my experience here in case it can help someone else. The upgrade to Device 3.1 as BillyMFH said changed the way tokens are created. The blog post has a lot of information in it, and the solution to the problem in this answer is a temporary one.
If config.allow_insecure_token_lookup is set to false (and once this option is deprecated), the fix in my case was to update the email views Devise uses to send out links containing tokens.
Old Devise email views contain lines like this:
<p><%= link_to 'Change my password', edit_password_url(#resource, :reset_password_token => #resource.reset_password_token) %></p>
This uses the old token stored in the DB. Now you simply do this:
<p><%= link_to 'Change my password', edit_password_url(#resource, :reset_password_token => #token) %></p>
Just replace the old token from resource with the new instance variable #token
The link in the blog post to this change is highlighted here: Devise email view token change
This likely has to do with the security updates in Devise 3.1. The token that is sent to the user does not match the one that is in the database. You could turn off this feature by including this in your devise initializer:
config.allow_insecure_token_lookup = true
But it would be best to just delete the user and create a new one with the new token system.
See this blog post about the security changes in Devise 3.1: http://blog.plataformatec.com.br/2013/08/devise-3-1-now-with-more-secure-defaults/ You're
looking for the section titled "Store digested tokens in the database"
Just you need to make the following changes while you update devise or else
In following line in file app/views/devise/mailer/confirmation_instructions.html.erb
<p><%= link_to 'Confirm my account', confirmation_url(#resource, :confirmation_token => #resource.confirmation_token) %></p>
with new line as -
<p><%= link_to 'Confirm my account', confirmation_url(#resource, :confirmation_token => #token) %></p>
This will definitely solve your problem, it just work for me.
Cheers!
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.