On Sep 26, 2012, at 11:52 PM, Avi wrote:
> Currently I am using CanCan.
> Can you please explain a bit more on your solution?
#users_controller.rb
before_filter :authenticate_impersonator!, :only => [:index, :impersonate, :stop_impersonating]
def impersonate
session[:impersonating] = params[:practice_id]
redirect_to( '/calendar' )
end
def stop_impersonating
session[:impersonating] = nil
redirect_to( '/users/index' )
end
def authenticate_impersonator!
redirect_to(:root) unless (can? :impersonate, User)
end
#application_controller.rb
helper_method :current_practice
def current_practice
if session[:impersonating]
Practice.find session[:impersonating]
else
current_user.practice
end
end
#views/layouts/index.html.erb
<%- if session[:impersonating] -%>
<div id="impersonating">
<p>Currently impersonating <strong><%=
current_practice.name %></strong> <%= link_to "Stop Impersonating", "/users/stop_impersonating", :class => "form_button delete" %></p>
</div>
<%- end -%>
Everything in this solution centers around the current_practice helper, which is where I used the session to side-step the current user and pretend to be another.
Walter
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To post to this group, send email to
rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to
rubyonrails-ta...@googlegroups.com.
> To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/udMtXUaASAoJ.
> For more options, visit
https://groups.google.com/groups/opt_out.
>
>