Skip to content

Instantly share code, notes, and snippets.

@markfeedly
Created July 3, 2014 08:04
Show Gist options
  • Save markfeedly/5c1c1d8c3b765d66fa5e to your computer and use it in GitHub Desktop.
Save markfeedly/5c1c1d8c3b765d66fa5e to your computer and use it in GitHub Desktop.
problem invoking devise effectively from a controller
current code https://github.com/markfeedly/social-museum/tree/update-mail
rake routes
user_session POST /users/sign_in(.:format) devise/sessions#create
devise sessions controller
# POST /resource/sign_in
def create
self.resource = warden.authenticate!(auth_options)
set_flash_message(:notice, :signed_in) if is_flashing_format?
sign_in(resource_name, resource)
yield resource if block_given?
respond_with resource, :location => after_sign_in_path_for(resource)
end
mods to ApplicationController
def store_location
# store last url - this is needed for post-login redirect to whatever the user last visited.
return unless request.get?
if request.path != "/users/sign_in" &&
request.path != "/users/sign_up" &&
request.path != "/users/password/new" &&
request.path != "/users/sign_out" &&
!request.xhr? # don't store ajax calls
session[:previous_url] = request.fullpath
Rails.logger.level = 0
logger.debug "%%%%%%%%%%%%%%%%%%%%%%%%%%% store_location #{session[:previous_url]} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
end
end
def after_sign_in_path_for(resource)
Rails.logger.level = 0
logger.debug "%%%%%%%%%%%%%%%%%%%%%%%%%%% after_sign_in_path_for #{session[:previous_url]} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
session[:previous_url] || root_path
end
rake routes output again
GET /pages/:id/unsubscribe-via-email(.:format) pages#unsubscribe_via_email
Demonstrable incompetence in my PagesController, where obvs I want to ensure someone is logged in
def unsubscribe_via_email # /pages/:id/unsubscribe-via-email
if current_user
render :confirm_unsubcribe
else
store_location
render 'devise/sessions/new' # <----------- arrugh I don't know what to put here, certainly its not a redirect
end
end
where visiting /pages/page_stub_here/unsubscribe-via-email invokes app/views/devise/sessions/new.html.haml but without a resource for this line in the haml
= simple_form_for(resource, :as => resource_name, :url => session_path(resource_name), :html => {:class => 'form-vertical'}) do |f|
where of course I have not supplied resource. I v strongly suspect the clue I need to solve this is in the devise sessions controller, but I wonder if there is a little change I can make to my unsubscribe_via_email action in my page controller
@workmad3
Copy link

workmad3 commented Jul 3, 2014

change

def unsubscribe_via_email # /pages/:id/unsubscribe-via-email
  if current_user
    render :confirm_unsubcribe
  else
    store_location
    render 'devise/sessions/new' 
  end
end

to

before_filter :authenticate_user!, only: [:unsubscribe_via_email]

def unsubscribe_via_email # /pages/:id/unsubscribe-via-email
  render :confirm_unsubcribe
end

and, for future reference, yes you did want a redirect ;) that 'store_location' line stores the current location in the session so that devise can redirect back to the same place after you redirect to the devise login page and successfully authenticate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment