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