What I thought would be a simple problem is giving me some trouble. I have an application with Devise/Omniauth for authentication. When a new user signs up and logs in, I want them to be directed to a couple of questions (in succession) asking:
- what beverages do they like? (coffee, drink, both)
- what neighborhoods are convenient for them?
My user model has a “preferred_beverage” attribute along with three preferred_neighborhood_# attributes (preferred_neighborhood_one, preferred_neighborhood_two, preferred_neighborhood_three).
When a user signs in, I want to check if they are a new user and if so, direct them to a page with question 1:
<div class="center hero-unit">
<h2>What do you like to drink?</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
<%= f.radio_button :preferred_beverage, 'Coffee', :checked => true %>
<%= label :preferred_beverage, 'Coffee' %>
<%= f.radio_button :preferred_beverage, 'Drink' %>
<%= label :preferred_beverage, 'Drink' %>
<%= f.radio_button :preferred_beverage, 'Both' %>
<%= label :preferred_beverage, 'Both' %>
<%= f.submit "Next", class: "btn btn-large btn-primary" %>
<% end %>
</div>
The problem is that, upon submitting this form, even though the user’s “preferred_beverage” attribute is saving, I cannot properly overwrite the default Devise “update” method, which seems to be handling any modifications to User and redirecting to the root_path. I want to redirect straight to my /app/views/neighborhood_survey/new.html.erb form upon submission of the ‘preferred_beverage’.
What exactly do I need to put in the “update” section of this registrations_controller.rb that I am using to override the default Devise registrations_controller?
app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def create
super
session[:omniauth] = nil unless @user.new_record?
end
def update
end
private
def build_resource(*args)
super
if session[:omniauth]
@user.apply_omniauth(session[:omniauth])
@user.valid?
end
end
end
I completed the below to ensure that my registrations_controller was overwriting the Devise registrations controller with the following:
routes.rb
devise_for :users, path_names: {sign_in: "login", sign_out: "logout"},
controllers: {omniauth_callbacks: "omniauth_callbacks", :registrations => "registrations"}
Any input is appreciated!
Thank you.
You should not be overriding devise update action, you need to be overriding the
route of devise. You will be able to redirect the user wherever you like after he signs in. You should put this in your application controller.
http://rubydoc.info/github/plataformatec/devise/master/Devise/Controllers/Helpers#after_sign_in_path_for-instance_method