问题
Been looking for past 3 hours... setting up confirmable email it protrayed as being easy but its is FAR from that. Anyway, I have fought through slew of errors and undefined maethods and variables and have wrestled this thing to send me something... However I cannot confirm it. I have looked under the hood in the confirmations controller to no avail... It just does not confirm right. I dont know what I am doing wrong here but it consistently redirects me to "Resend confirmation instructions" page...
UPDATE: Got hold of the error that is being generated at its the infamous Invalid token error. Again hour of research solved nothing I keep hearing that you need to use @token just like I do in my view... and it supposed to make it work but it does not.
Controller: Default Devise confirmations controller:
class ConfirmationsController < Devise::ConfirmationsController
# GET /resource/confirmation/new
def new
self.resource = resource_class.new
end
# POST /resource/confirmation
def create
self.resource = resource_class.send_confirmation_instructions(resource_params)
yield resource if block_given?
if successfully_sent?(resource)
respond_with({}, location: after_resending_confirmation_instructions_path_for(resource_name))
else
respond_with(resource)
end
end
# GET /resource/confirmation?confirmation_token=abcdef
def show
self.resource = resource_class.confirm_by_token(params[:confirmation_token])
yield resource if block_given?
if resource.errors.empty?
set_flash_message(:notice, :confirmed) if is_flashing_format?
respond_with_navigational(resource){ redirect_to after_confirmation_path_for(resource_name, resource) }
else
respond_with_navigational(resource.errors, status: :unprocessable_entity){ render :new }
end
end
protected
# The path used after resending confirmation instructions.
def after_resending_confirmation_instructions_path_for(resource_name)
new_session_path(resource_name) if is_navigational_format?
end
# The path used after confirmation.
def after_confirmation_path_for(resource_name, resource)
if signed_in?(resource_name)
authenticated_root_path(resource)
else
unauthenticated_root_path
end
end
end
Mailer view:
<p>Welcome <%= @email %>!</p>
<p>You can confirm your account email through the link below:</p>
<p><%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %></p>
Routes piece:
devise_for :users, controllers: { registrations: 'registrations', sessions: 'sessions', confirmations: 'confirmations'}
devise_scope :user do
authenticated :user do
root :to => 'homepage#index', as: :authenticated_root
end
unauthenticated :user do
root :to => 'devise/sessions#new', as: :unauthenticated_root
end
end
New Confirmations vew
<h2>Resend confirmation instructions</h2>
<%= form_for(resource, as: resource_name, url: confirmation_path(resource_name), html: { method: :post }) do |f| %>
<div><%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %></div>
<div><%= f.submit "Resend confirmation instructions" %></div>
<% end %>
<%= devise_error_messages!(:email) %>
<%= render "devise/shared/links" %>
Also, I have reworked devise error helper to have it display custom errors (Dont know may that is what messing it up but here it is:
module DeviseHelper
def devise_error_messages!(field)
return nil if resource.errors.empty?
messages = resource.errors.full_messages_for(field).map { |msg| content_tag(:li, msg) }.join
if resource.errors.full_messages_for(field) != []
html = <<-HTML
<div class="alert alert-error alert-block"> <button type="button"
class="close" data-dismiss="alert">x</button>
#{messages}
</div>
HTML
html.html_safe
else
return nil
end
end
end
回答1:
Ok so after 10 hours of fightting I decided to restrt from scratch and have made it work on local as well as on heroku. Follow this tut for Rails 4 and Devise 3.2.4: https://github.com/plataformatec/devise/wiki/How-To:-Add-:confirmable-to-Users. Then i got Undefined router error for a good while so I look at the registrations controller and replaced the 6 or 7 lines found in the default version of it heres the link to another tut that explains it better https://github.com/plataformatec/devise/wiki/How-To%3a-Redirect-to-a-specific-page-on-successful-sign-up-%28registration%29:
Registrations controller action (Working)
# The path used after sign up for inactive accounts. You need to overwrite
# this method in your own RegistrationsController.
def after_inactive_sign_up_path_for(resource)
after_sign_in_path_for(resource)
end
Viola! It went through without any hassle. So something that should only take 5 min only taken me 10 hours. That's efficiency right there!
Also I got it to work with mobile format just in case some of you have problems with it:
confirmations controller:
# GET /resource/confirmation?confirmation_token=abcdef
def show
self.resource = resource_class.confirm_by_token(params[:confirmation_token])
yield resource if block_given?
if resource.errors.empty?
set_flash_message(:notice, :confirmed) if is_flashing_format?
respond_to do |format|
format.html{redirect_to unauthenticated_root_path}
format.mobile {redirect_to unauthenticated_root_path, notice: 'Confirmation success!'}
end
else
respond_to do |format|
format.html{render :new}
format.mobile {redirect_to unauthenticated_root_path, alert: 'Failure, already confirmed'}
end
end
end
If you have more technically correct answer post it and I will accept it if its better than mine!
来源:https://stackoverflow.com/questions/25128144/devise-confirmable-token-is-invalid-error