after_sign_in_path_for action not loading Geokit in Rails - ruby-on-rails

Im trying to refresh the Geokit location to session after the user sign in. I have the following code.
# app/controllers/application_controller.rb
def after_sign_in_path_for(resource_or_scope)
session[:geo_location] = User.geocode(current_user.city)
end
But I'm getting the following error.
NoMethodError in Devise::SessionsController#create
undefined method `model_name' for Geokit::GeoLoc:Class
Seems like Geokit is not loading before the devise controller. Any idea?

The problem is after_sign_in_path_for must return a valid an url object and you are not doing it check its documentation.

Related

Rapidfire and Devise

I implemented Devise and went through the instructions to install Rapidfire
and for the application controller I have
def current_user
current_user #rb:7
end
def can_administer
true # just for right now...
end
but on the page I get a something went wrong and if I look in the console it says
ActionView::Template::Error (stack level too deep)
app/controllers/application_controller.rb:7
which is the current_user line.
Can anyone tell me whats going on?
You are creating a method called current_user, and you are returning the value of current_user.
Ruby doesn't require that you use parenthesis when calling methods.
current_user
is the same thing as
current_user()
You are calling the current_user function over and over and over again.
There is no reason for you to define a method called current_user, when devise gives that to you.

Devise URL Helpers are not being generated on reset password procedure

I'm trying to implement reset password feature from Devise on my Rails App. It works fine but after sending email instructions it redirects to a wrong URL (api/sessions/new insteadof users/sign_in). Looking on devise code I found that it calls this method to get the url:
# The path used after sending reset password instructions
def after_sending_reset_password_instructions_path_for(resource_name)
new_session_path(resource_name) if is_navigational_format?
end
The resource_name is "user" and the new_session_path("user") is returning the matching route new_session_path on routes.rb file. The expected response should be new_user_session_path.
There is an URL Helper on Devise that should translate new_session_path(:user) to new_user_session_path but it is not working: http://www.rubydoc.org/github/plataformatec/devise/Devise/Controllers/UrlHelpers#generate_helpers%21-class_method
Does anyone know why ? Should I call manually the generate_helpers! method ?
My Rails version is 3.2.14 and Devise 3.2.2.
Best regards !
The problem was that I had a SessionsController which was overriding the Devise SessionsController and the routes paths. After rename my SessionsController It worked fine.

A before_filter for Omniauth Facebook?

For devise there is a very easy to use Before_filter to use in the controllers.
For some reason I can't get this to work for the Omniauth_facebook Gem. I followed the Railscast on Facebook Omniauth and also
before_filter :authenticate
def authenticate
redirect_to :login unless User.find_by_provider_and_uid(auth["provider"], auth["uid"])
end
end
but I get an error:
NameError in PostsController#new
undefined local variable or method `auth' for #<PostsController:0x007f9fbfa7ee58>
Any thoughts?
It can't find the variable named auth. So you need to check that auth variable initialized somewhere or not. As per my view, OmniAuth Facebook gem is storing the authenticated data in request env. Please refer this: https://github.com/mkdynamic/omniauth-facebook#auth-hash.
before_filter will execute before serving the request. so may be that causes the issues.
Hope that helps!!!
I think the auth variable you're after is request.env["omniauth.auth"]

Rails Devise reset_authentication_token

I am using Rails 3.2.2 and Devise 2.0.4 with token_authenticatable turned on and everything work well. Now I want to do the following
When a user logs in or logs out reset_authentication_token for the users
For this I need to override the SessionsController but I dont know where to put the call to reset_authentication_token
(Note I want this only when a user is successfully logged in or out)
You can override the sign_in helper of Devise in ApplicationController:
def sign_in(resource_or_scope, *args)
super
current_user.reset_authentication_token!
end

Rails Catchall Route (app allows username in URL)

I have a simple blogging app where a user's username is part of the URL (i.e. myapp.com/username). How do I create a catchall route for all usernames not found. Currently, an invalid username causes:
NoMethodError in UsersController#show
undefined method `articles' for nil:NilClass
I'd like that to redirect to the 404 page.
Try this one
Write following function into your application controller
def not_found
raise ActionController::RoutingError.new('Not Found')
end
Then write down something as below into your users controller
User.find_by_username(params[:username]) || not_found
Hope, this will help you.
Looks like this is not the case in Production, which does indeed redirect to the 404 for invalid routes.

Resources