I've had the following setup to send the user a welcome email after a successful registration.
class RegistrationsController < Devise::RegistrationsController
def create
super
unless #user.invalid?
UserMailer.delay.welcome(#user)
end
end
end
I'm on Rails 4.1.4 and I recently updated all gems, which updated Devise to 3.2.4. After the update, the above started throwing the following error:
wrong number of arguments (1 for 2)
app/controllers/registrations_controller.rb:4:in `create'
Seems like calling super is what is breaking things.
Please advice.
This wasn't Devise fault, but rather the following Turbolinks change broke redirect_to https://github.com/rails/turbolinks/commit/153f1b0f04c718442cfd73365a2778dfe1a1c5c7
Rolling back to Turbolinks 2.2.2 or 3f2b6e752acde1d9a59a75c48401dfb152afe154 solved the problem.
Related
I had a rails app which was running perfectly with devise 3 and rails 4. I updated rails to 5 and it gave DEPRECATION WARNING: alias_method_chain is deprecated. So i had to change the devise version to 4.0.0.rc1.
Now it seems like the devise is not generating the methods like authenticate_user!, is_admin etc..
I am getting the following error
Before process_action callback :authenticate_user! has not been defined (ArgumentError)
I tried everything including, uninstall devise and install again, then tried to create a separate model from scratch and still the methods are not defined.
Any ideas please ?
The answer for this is to simply change:
skip_before_action :authenticate_user!
to ---
skip_before_action :authenticate_user!, raise: false
https://github.com/plataformatec/devise/issues/4207
https://github.com/thoughtbot/clearance/issues/621
I could identify the issue, in rails 5, there will be an exception if the skipping method is not defined at the time of calling unless we add :unless condition.
Until rails5 support lands in devise itself, you can try using this fork:
gem 'devise', github: 'twalpole/devise', branch: 'rails5'
I've followed Michael Hartl's Ruby on Rails Tutorial making an app like Twitter and I want to change it so that all new users to automatically 'follow' the Admin/s.
I've tried to do it following answers to similar questions here and here, but they throw errors on account creation. Here's the relevant section of my user controller.
Edit: find_all_by_admin is deprecated in Rails 4.2.0 as explained in the selected answer by Vinay.
controllers/users_controller.rb
class UsersController < ApplicationController
...
def follow_admins
admins = User.find_all_by_admin(true) # EDIT - Deprecated: May have worked prior to rails 4.2
admins.each do |admin|
self.follow!(admin)
end
end
The error message is
NoMethodError in UsersController#create
undefined method `find_all_by_admin' for #
It seems to me that there is no column with admin name in your User model as we might can see Michael Hartl's sample_app_3rd_edition
So in order to make follow_admins method work you need to add admin column in users table type boolean and default: false.
def follow_admins
admins = User.find_all_by_admin(true) # would be worked in rails 4.0 not rails 4.2.2
admins = User.where(admin: true) # Should be work in rails 4.2.2
# Most of the Dynamic finder has been removed form rails 4.2.2
admins.each do |admin|
self.follow!(admin)
end
end
note As I mention in my answer default to false ,It is not mandatory but as you are following Michael Hartl's Ruby on Rails Tutorial It good to go accordingly .
hope this answer would help you !!!
It's likely you never defined the find_all_by_admin method in the User model.
Open the User model file and check if the method is there. If not, define it as a class method.
I had override Devise's RegistrationsController to send a welcome email when a user signup. Trying to use Roadie for some inline style modification for the email before sending out but an uninitialized constant Users::RegistrationsController::Roadie was shown on the line calling Roadie:
app/controllers/users/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
def create
# Call the original Devise Create
super
if resource.save
## New version with inline css styling
## Things done in the hacky way
email = UserNotifier.send_signup_email(resource)
doc = Roadie::Document.new email.body.to_s
doc.add_css(Rails.application.assets["email"].to_s)
email.body = doc.transform
email.deliver
..
So, when I call Roadie above, instead of the Roadie Class in the gem it try to call Users::RegistrationsController::Roadie. Gem was installed when checking with bundle show. Have been trying to look into devise code to see if there're kind of scooping (I am not sure there's such thing in Controller indeed) but no luck as well. I guess it should be super simple so hope someone could help!
And of course.. The above code works in console. :)
Thank you!
I am using devise 3.5.1, rails 4.0.0 and ruby 2.0.0-p0.
If a user is not signed in and tries to open a private page which requires authentication, let's say lvh.me:3000/users/1/edit, then devise will add users/1/edit in session against the key user_return_to. The problem is that when the user gets redirected to the login page after authentication failure then user_return_to value in session gets reset.
I have this problem all over my app. If I store a value in session and redirect to another path from the same action then the session value disappears.
Note: I have upgraded my rails app from (rails 3.0.4, ruby 1.8.7) to (rails 4.0.0, ruby 2.0.0).
Sorry for any ambiguities in the question. If you have any question please comment it.
UPDATE:
In my application controller, I've added a before filter to authenticate user. Only relevant code is shown in application controller.
application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :authenticate_user!, :unless => :devise_controller?
# rest of the code
end
I'm using a custom failure class which inherits Devise::FailureApp. The code looks like this.
custom_failure.rb
class CustomFailure < Devise::FailureApp
def redirect_url
eval "new_user_session_path"
end
def redirect
store_location!
flash[:alert] = i18n_message
# session[:user_return_to] returns '/foobar' here.
redirect_to redirect_url
end
end
I have commented a line in above code. session[:user_return_to] is available at that line but after the redirect on very next line, the session[:user_return_to] becomes nil.
I've upgraded devise from 1.5.3 to 3.5.1.
I'm adding the solution here in case anybody else encounters the same problem.
I had a resource named asset in my application and it was conflicting with the asset pipeline. The session was getting reset whenever my URL contained 'assets' e.g. '/assets'. So if you have a resource named asset then you have to change the prefix of your asset pipeline. To do so, add the following line in your application.rb file.
config.assets.prefix = '/static_assets'
Also you have to rename the asset pipeline folder to static_assets.
Please note that this solution only applies if you have a resource named Asset in your application and the rails version is 3.1+
I had a two month break using Rails and now its biting me when I return!
I am trying to get CanCan to work on Rails 3.1 and have viewed the railscast and then followed their instructions on the cancan git page. However when I try run the homepage it gives me the following error:
Routing Error
undefined local variable or method `authorize_resource' for StaticController:Class
How do I make this stop? Its as if cancan is not loaded, but I have installed it using bundler and it doesnt mention that I have to do anything else to include it.
Anyone have any ideas?
Sorry my bad! Too much coffee resulted in me not reading all the instructions for how to install it.
I needed to add the following to my ApplicationController:
def current_ability
#current_ability ||= Ability.new(current_user)
end
and then I needed to use
class StaticController < ApplicationController
authorize_resource :class => false
Because in this particular example it was just a static simple homepage that sits infront of a more complicated web app.
Thanks for the help.