Devise : Sign in from a controller - ruby-on-rails-3

I need to sign in fron non-devise controller. I found this link from devise wiki, but I get error :
wrong number of arguments (2 for 0)
I did exactly like in the link. What went wrong? Thanks
My code :
sign_in(:site, Site.find(params["si"]))

sign_in(:user, User.find(params[:id])
If you do a binding.pry after you'll see that current_user is set.
For some reason, doing a redirect after this will make current_user nil.
That's why I set something in the session like this and then redirect, and then check if that session id is set and then use it to do the above code:
session[:new_user_id] = params[:id]

you need to pass agruments post you code for clarity
sign_in(:user, User.find(params[:id]))

I'm probably late. But had the same problem and as it turns out I had a sign_in method in my users controller.
Did you check for a sign_in method in the controller where you are calling the sign_in method?

Related

how to reference acts_as_tenant current_tenant from cancan ability.rb

I have some overall permissions set against the current_tenant - I can reference these from within a controller i.e.
current_tenant.has_some_capability?
works fine.
I would like to wrap this up using CanCan, if I put the following in my ability.rb I get undefined local variable or method `current_tenant' for #
if current_tenant.some_capability?
can :manage, Whatever
end
I believe that CanCan expects current_user, try this:
current_user = current_tenant
Read the first part of the Getting Started
If you can't access current_tenant from your ability.rb, try setting #current_tenant as a before_filter in your application controller:
before_filter do
#current_tenant = current_tenant
end

Active admin change default model admin_user

I'm starting my first project with Active Admin.
To use another model for my users I use the following command :
rails generate active_admin:install User
After this I make this change in active_admin initializer :
config.authentication_method = :authenticate_user!
config.current_user_method = :current_user
I'm correctly login my application but on the home page I get this error :
undefined method `destroy_admin_user_session_path' for #<ActiveAdmin::Views::HeaderRenderer:0x007ff8fa086a60>
How can I fix it properly ?
Solved by editing initializer :
config.logout_link_path = :destroy_user_session_path
This is addition to #Awea answer. Use togather with that.
Check rails routing table for destroy_user_session.
For example devise token auth make route table entry like this:
destroy_user_session DELETE /auth/sign_out(.:format) devise_token_auth/sessions#destroy
But default method for activeadmin logout link is :get and it will not work.
To make it worked properly add to config/initializers/active_admin.rb also and:
config.logout_link_method = :delete

Rspec 2 + Devise + Cancan - Factorygirl = Controller Testing not working

I'm trying to move to rspec for testing, but I can't get controller testing to work with user authentication. Testing a route that doesn't require a user works.
require 'spec_helper'
describe UddsController do
include Devise::TestHelpers
render_views
before (:each) do
#user = User.new(:email => "test#user.com", :username => "test123")
#user.roles << Role.find_or_create_by_name("admin")
#user.save
sign_in #user
end
it "should get index" do
get :index
response.should be_success
end
I just get
2) UddsController should get index
Failure/Error: response.should be_success
expected success? to return true, got false
# ./spec/controllers/udds_controller_spec.rb:21
I'm not using factory_girl. All the example fixes here and on google seem to. I don't see why what I've done wouldn't work. Error is useless in debugging.
I know this question has been asked to death. I've read every blog and forum I can find. I'm at a dead end.
It might be redirecting. be_success returns false for any non-2xx status codes (like 302). Try checking the value of response.status -- if it's redirecting, it probably means your authentication or authorization scheme is producing an unexpected result.
Try adding puts response.body before your assertion, and see what the page you are requesting says. If you can't figure it out from there, perhaps edit your question to include the output?
If you are using devise's "confirmable" module which makes users click a link in an email to confirm their address, then when you use "sign_in" in the tests you also have to fake email confirming. It looks something like this:
sign_in #user
#user.confim!

Rails 3 and Devise. Kill current session

I got an Rails 3 app that uses Devise. I am just wondering how I can "kill" the current session?
This works but I do not know what it does
Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name)
This does not work
current_user = nil
This does not work either
session[:current_user] = nil
you can do like this
sign_out current_user
or
sign_out :user # sign_out(scope)
or
sign_out #user # sign_out(resource)
You probably want the sign_out method, and pass either the user or scope (eg :user) that you want to sign out.
Check out the Devise Ruby Doc for more information.

authlogic_oid redirecting to /user_sessions#index instead of /user_session#create

I'm writing a rails3 app using authlogic and authlogic-oid to allow the user to login and register using their LinkedIn account. I'm able to call out to LinkedIn to authenticate but the issue I'm having is that when the call returns to my rails app, it is calling a GET on /user_sessions and invoking the index controller instead of executing the remainder of the create controller. Here's the controller:
# POST /user_sessions
def create
#user_session = UserSession.new(params[:user_session])
#user_session.save do |result| # <-- redirects to api.linkedin.com here
if result
flash[:success] = "Login successful!"
redirect_back_or #user_session.user_path
else
flash.now[:error] = "Invalid username/password combination."
#title = "Sign in"
render 'new'
end
end
end
Everything works great until if result which never gets called because the program resumes execution in the index controller instead of picking up where it left off. Any ideas on how to fix this would be greatly appreciated.
Edit: it seems my assumption that the controller was not completing execution was wrong. I've put in a bunch of debug statements and learned that the the program does resume execution in this controller after #user_session.save but it is not executing either condition on the if statement. I'm very confused on this one.
if you don't want to use index method in your UserSessionsController then write this: resources :user_session in your route.rb . If you'll use singular form then route can map CRUD (create, read, update, delete) and if you'll use plural of it then it will bind CRUD with index method of your controller.