DEVISE after_inactive_sign_up_path_for not being called - ruby-on-rails

Environment: RAILS 3.2 + DEVISE for auth + Invitable + Confirmable add-ons.
Using devise (2.2.3)
Using devise-i18n (0.6.5)
Using devise_invitable (1.0.3)
I am trying to redirect to a specific location after ACCEPT (TO SIGN UP), but only after_sign_in_path_for seems to be called after SIGN IN and ACCEPT.
I haven't been able to have after_accept_path_for working.
It continues to redirect to the "after sign in" location.
HERE THE CODE
In my routes.rb:
devise_for :users,
:controllers => { :registrations => 'registrations', :invitations => 'invitations' }
rake routes give me this:
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
user_confirmation POST /users/confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /users/confirmation/new(.:format) devise/confirmations#new
GET /users/confirmation(.:format) devise/confirmations#show
accept_user_invitation GET /users/invitation/accept(.:format) devise/invitations#edit
user_invitation POST /users/invitation(.:format) devise/invitations#create
new_user_invitation GET /users/invitation/new(.:format) devise/invitations#new
PUT /users/invitation(.:format) devise/invitations#update
In my registration controller:
class RegistrationsController < Devise::RegistrationsController
# clear session
def create
super
session[:omniauth] = nil unless #user.new_record?
end
#protected
# after_sign_up_path_for doesn't seem to be called when using Confirmable module
# def after_inactive_sign_up_path_for(resource)
# #me_path
# session[:user_return_to].nil? ? me_path : session[:user_return_to]
# end
private
def build_resource(*args)
super
if session[:omniauth]
#user.apply_omniauth(session[:omniauth])
#user.valid?
end
end
end
Also
class Users::InvitationsController < Devise::InvitationsController
protected
def after_accept_path_for
session[:user_return_to].nil? ? me_path : session[:user_return_to]
end
end
In my application controller, (I left intentionally some commented code I tried to make it work):
def store_location
session[:user_return_to] = request.fullpath
end
# def after_sign_up_path_for
# me_path
# end
#
# def after_inactive_sign_up_path_for(resource)
# me_path
# #session[:user_return_to].nil? ? me_path : session[:user_return_to]
# end
# https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-up-(registration)
def after_sign_in_path_for(resource)
me_path
#dashboard_path
#session[:user_return_to].nil? ? dashboard_path : session[:user_return_to]
end
Any suggestions?
ADDED DEBUGGIN REDIRECTS
Add this to my application_controller
def redirect_to_with_logging(*args)
logger.debug "Redirect: #{args.inspect} from #{caller[0]}"
redirect_to_without_logging *args
end
alias_method_chain :redirect_to, :logging
After Sign in, works like a charm
Started POST "/users/sign_in" for 127.0.0.1 at 2013-04-25 14:20:04 +0200
Processing by Devise::SessionsController#create as HTML
[... I removed some of the Session creation info ...]
Redirect: ["/dashboard"] from /Users/joel/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.11/lib/action_controller/metal/responder.rb:135:in `redirect_to'
Redirected to http://localhost:3000/dashboard
Completed 302 Found in 968ms (ActiveRecord: 0.0ms)
DOCS:
After sign in
After sign up
After Accept <====

Override after_accept_path_for in Invitations controllers
class Users::InvitationsController < Devise::InvitationsController
protected
def after_accept_path_for(resource)
me_path
end
end
IMPORTANT:
put this file in 'controllers/users' directory
Fix the routes.rb to use the Users::InvitationsController
devise_for :users, :controllers => { :registrations => 'registrations', :invitations => 'users/invitations' }

You may put redirect_to my_specific_url at the end of sign_up controller's method, to redirect after signing up

Related

RuntimeError in Devise::InvitationsController#create, Could not find a valid mapping for nil

I am using devise_invitable gem in my rails 5 application. Every time after submitting the "Send an Invitation" button, I get the error RuntimeError in Devise::InvitationsController#create, Could not find a valid mapping for nil
Here is the code of application_controller.rb file:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
def authenticate_inviter!
unless current_user.role=='Manager'
if current_user.role == "Developer"
redirect_to root_path, :alert => "Access Denied! Only Manager Can Add New Developer"
end
super
end
end
protected
def configure_permitted_parameters
added_attrs = [ :email, :password, :password_confirmation ]
devise_parameter_sanitizer.permit :sign_up, keys: added_attrs
devise_parameter_sanitizer.permit :account_update, keys: added_attrs
devise_parameter_sanitizer.permit :accept_invitation, keys: [:email]
end
end
Here is the Code of User.rb:
class User < ApplicationRecord
include DeviseInvitable::Inviter
devise :invitable, :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
end
Here is Route.rb
Rails.application.routes.draw do
devise_for :users
root 'dashboard#index'
end
Here is all routes:
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
user_password PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
POST /users/password(.:format) devise/passwords#create
cancel_user_registration GET /users/cancel(.:format) devise_invitable/registrations#cancel
new_user_registration GET /users/sign_up(.:format) devise_invitable/registrations#new
edit_user_registration GET /users/edit(.:format) devise_invitable/registrations#edit
user_registration PATCH /users(.:format) devise_invitable/registrations#update
PUT /users(.:format) devise_invitable/registrations#update
DELETE /users(.:format) devise_invitable/registrations#destroy
POST /users(.:format) devise_invitable/registrations#create
accept_user_invitation GET /users/invitation/accept(.:format) devise/invitations#edit
remove_user_invitation GET /users/invitation/remove(.:format) devise/invitations#destroy
new_user_invitation GET /users/invitation/new(.:format) devise/invitations#new
user_invitation PATCH /users/invitation(.:format) devise/invitations#update
PUT /users/invitation(.:format) devise/invitations#update
POST /users/invitation(.:format) devise/invitations#create
root GET / dashboard#index
rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update
rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
I haven't created any custom invitations controller.
I just run the following to setup and configure devise_invitable
gem 'devise_invitable'
bundle install
rails generate devise_invitable:install
rails generate devise_invitable User
rails db:migrate
rails generate devise_invitable:views
restart the rails server
Here is the picture of the complete error:
see the picture of the complete error
How can I get rid of this problem. Please help me
Finally I have solved this issue by the help of Scambra
I have just changed the logic of my Application Controller
From:
def authenticate_inviter!
unless current_user.role=='Manager'
if current_user.role == "Developer"
redirect_to root_path, :alert => "Access Denied! Only Manager Can Add New Developer"
end
super
end
end
To:
def authenticate_inviter!
if current_user.role == "Developer"
redirect_to root_path, :alert => "Access Denied! Only Manager Can Add New Developer"
end
super
end

Devise omniauth is redirecting to failure method in callback controller even after successful authentication.?

I am working on devise omniauth to authenticate user using facebook. I have implemented it using the documentation https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview
It is not working fine. When I login using my facebook account then it takes me to the "failure" method in the callback controller.
But the interesting thing is that when I open facebook it open without any problem means it is authenticating my account.
I have checked it so many times by login and logout from facebook. But the problem is that If it is authenticating facebook account then why it takes me to "failure method".
here is my code
callback controller.
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
#user = User.from_omniauth(request.env["omniauth.auth"])
if #user.persisted?
sign_in_and_redirect #user, :event => :authentication #this will throw if #user is not activated
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
def failure
render plain: params.inspect
# redirect_to root_path
end
end
User model
class User < ApplicationRecord
# Include default devise modules. Others available are:
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable
devise :omniauthable, :omniauth_providers => [:facebook]
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.name = auth.info.name # assuming the user model has a name
user.image = auth.info.image # assuming the user model has an image
# If you are using confirmable and the provider(s) you use validate emails,
# uncomment the line below to skip the confirmation emails.
# user.skip_confirmation!
end
end
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
user.email = data["email"] if user.email.blank?
end
end
end
end
route code
devise_for :users, controllers: {confirmations: 'confirmations',registrations: 'users/registrations',omniauth_callbacks: 'users/omniauth_callbacks' } do
#put "confirm_user", to: "confirmations#confirm_user"
get "confirmation", to: "confirmations#after_confirmation_path_for"
end
Facebook app setting screenshot is
https://www.dropbox.com/s/vn8cjpr5wyagkdp/Screenshot%202017-11-27%2017.04.28.png?dl=0
development logs after sign in are
Started GET "/users/auth/facebook" for 10.0.2.2 at 2017-11-28 14:13:37 +0000
Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Started GET "/users/auth/facebook/callback?code=AQA8oLlPsMLSvZf5NqZfOA0oJxDVKczwqqMIxWPD2dJoucpJl9T7MLTmf0mMDlOhgQPAhKa2f6My4vWGZmTWhVo6S5TbSQ3ELup1iBhDHTb869pMMo-ksa1Kh1gJDG65ZJxUj5vAe8jH-DX0eBRpf_ygZACCoFYFTiZdAIzCXQI7jfLaeqH70CqAffkGZczYzhjThM_NLol3Lzo18ZX_6_5n2-p7nMC3IKhmzDEyo_toyaI1telD3QMwa0re7GIu-UXKV4DQp-ClLT452Bigp9Fhs50wYm-Kl08E7195R2mpBESpB7Gu0moDbCgi61dEEk5u8GGfmm0Cxbu9Fcw1_Eu8" for 10.0.2.2 at 2017-11-28 14:13:40 +0000
Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by Users::OmniauthCallbacksController#failure as HTML
Parameters: {"code"=>"AQA8oLlPsMLSvZf5NqZfOA0oJxDVKczwqqMIxWPD2dJoucpJl9T7MLTmf0mMDlOhgQPAhKa2f6My4vWGZmTWhVo6S5TbSQ3ELup1iBhDHTb869pMMo-ksa1Kh1gJDG65ZJxUj5vAe8jH-DX0eBRpf_ygZACCoFYFTiZdAIzCXQI7jfLaeqH70CqAffkGZczYzhjThM_NLol3Lzo18ZX_6_5n2-p7nMC3IKhmzDEyo_toyaI1telD3QMwa0re7GIu-UXKV4DQp-ClLT452Bigp9Fhs50wYm-Kl08E7195R2mpBESpB7Gu0moDbCgi61dEEk5u8GGfmm0Cxbu9Fcw1_Eu8"}
Rendering text template
Rendered text template (0.0ms)
Completed 200 OK in 20ms (Views: 10.6ms | ActiveRecord: 0.0ms)
The result of params.inspect is as follows
<ActionController::Parameters {"code"=>"AQA8oLlPsMLSvZf5NqZfOA0oJxDVKczwqqMIxWPD2dJoucpJl9T7MLTmf0mMDlOhgQPAhKa2f6My4vWGZmTWhVo6S5TbSQ3ELup1iBhDHTb869pMMo-ksa1Kh1gJDG65ZJxUj5vAe8jH-DX0eBRpf_ygZACCoFYFTiZdAIzCXQI7jfLaeqH70CqAffkGZczYzhjThM_NLol3Lzo18ZX_6_5n2-p7nMC3IKhmzDEyo_toyaI1telD3QMwa0re7GIu-UXKV4DQp-ClLT452Bigp9Fhs50wYm-Kl08E7195R2mpBESpB7Gu0moDbCgi61dEEk5u8GGfmm0Cxbu9Fcw1_Eu8"} permitted: false>
The output of rake routes is
rake routes
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_facebook_omniauth_authorize GET|POST /users/auth/facebook(.:format) users/omniauth_callbacks#passthru
user_facebook_omniauth_callback GET|POST /users/auth/facebook/callback(.:format) users/omniauth_callbacks#facebook
user_twitter_omniauth_authorize GET|POST /users/auth/twitter(.:format) users/omniauth_callbacks#passthru
user_twitter_omniauth_callback GET|POST /users/auth/twitter/callback(.:format) users/omniauth_callbacks#twitter
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
user_password PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
POST /users/password(.:format) devise/passwords#create
cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel
new_user_registration GET /users/sign_up(.:format) users/registrations#new
edit_user_registration GET /users/edit(.:format) users/registrations#edit
user_registration PATCH /users(.:format) users/registrations#update
PUT /users(.:format) users/registrations#update
DELETE /users(.:format) users/registrations#destroy
POST /users(.:format) users/registrations#create
new_user_confirmation GET /users/confirmation/new(.:format) confirmations#new
user_confirmation GET /users/confirmation(.:format) confirmations#show
POST /users/confirmation(.:format) confirmations#create
new_model_session GET /models/sign_in(.:format) devise/sessions#new
model_session POST /models/sign_in(.:format) devise/sessions#create
destroy_model_session DELETE /models/sign_out(.:format) devise/sessions#destroy
new_model_password GET /models/password/new(.:format) devise/passwords#new
edit_model_password GET /models/password/edit(.:format) devise/passwords#edit
model_password PATCH /models/password(.:format) devise/passwords#update
PUT /models/password(.:format) devise/passwords#update
POST /models/password(.:format) devise/passwords#create
cancel_model_registration GET /models/cancel(.:format) devise/registrations#cancel
new_model_registration GET /models/sign_up(.:format) devise/registrations#new
edit_model_registration GET /models/edit(.:format) devise/registrations#edit
model_registration PATCH /models(.:format) devise/registrations#update
PUT /models(.:format) devise/registrations#update
DELETE /models(.:format) devise/registrations#destroy
POST /models(.:format) devise/registrations#create
new_model_confirmation GET /models/confirmation/new(.:format) devise/confirmations#new
model_confirmation GET /models/confirmation(.:format) devise/confirmations#show
POST /models/confirmation(.:format) devise/confirmations#create
donations_donor_history GET /donations/donor_history(.:format) donations#donor_history
donations_donor_signup GET /donations/donor_signup(.:format) donations#donor_signup
POST /donations/donor_signup(.:format) donations#donor_signup
donations_sms_service POST /donations/sms_service(.:format) donations#sms_service
donations_create_user_account POST /donations/create_user_account(.:format) donations#create_user_account
donations_add_user_payroll POST /donations/add_user_payroll(.:format) donations#add_user_payroll
donations GET /donations(.:format) donations#index
edit_donation GET /donations/:id/edit(.:format) donations#edit
donation GET /donations/:id(.:format) donations#show
PATCH /donations/:id(.:format) donations#update
PUT /donations/:id(.:format) donations#update
DELETE /donations/:id(.:format) donations#destroy
campaign_donations POST /campaigns/:campaign_id/donations(.:format) donations#create
new_campaign_donation GET /campaigns/:campaign_id/donations/new(.:format) donations#new
campaign_donations_create_user_account GET /campaigns/:campaign_id/donations/create_user_account(.:format) donations#create_user_account
campaigns GET /campaigns(.:format) campaigns#index
POST /campaigns(.:format) campaigns#create
new_campaign GET /campaigns/new(.:format) campaigns#new
edit_campaign GET /campaigns/:id/edit(.:format) campaigns#edit
campaign GET /campaigns/:id(.:format) campaigns#show
PATCH /campaigns/:id(.:format) campaigns#update
PUT /campaigns/:id(.:format) campaigns#update
DELETE /campaigns/:id(.:format) campaigns#destroy
organizations GET /organizations(.:format) organizations#index
POST /organizations(.:format) organizations#create
new_organization GET /organizations/new(.:format) organizations#new
edit_organization GET /organizations/:id/edit(.:format) organizations#edit
organization GET /organizations/:id(.:format) organizations#show
PATCH /organizations/:id(.:format) organizations#update
PUT /organizations/:id(.:format) organizations#update
DELETE /organizations/:id(.:format) organizations#destroy
admins_social_sharing_switch POST /admins/social_sharing_switch(.:format) admins#social_sharing_switch
admins_error_detail GET /admins/error_detail(.:format) admins#error_detail
GET /admins/generate_report/:id(.:format) admins#generate_report
admins_create_company GET /admins/create_company(.:format) admins#create_company
POST /admins/create_company(.:format) admins#create_company
admins_revenue_detail GET /admins/revenue_detail(.:format) admins#revenue_detail
admins_create_account GET /admins/create_account(.:format) admins#create_account
admins_view_account GET /admins/view_account(.:format) admins#view_account
GET /admins/view_company/:id(.:format) admins#view_company
admins_donation_analysis GET /admins/donation_analysis(.:format) admins#donation_analysis
admins_link_expiry GET /admins/link_expiry(.:format) admins#link_expiry
admins_edit_profile GET /admins/edit_profile(.:format) admins#edit_profile
admins_update_profile POST /admins/update_profile(.:format) admins#update_profile
POST /admins/create_account(.:format) admins#create_account
admin_destroy GET /admins/:id(.:format) admins#destroy
admins GET /admins(.:format) admins#index
POST /admins(.:format) admins#create
new_admin GET /admins/new(.:format) admins#new
edit_admin GET /admins/:id/edit(.:format) admins#edit
admin GET /admins/:id(.:format) admins#show
PATCH /admins/:id(.:format) admins#update
PUT /admins/:id(.:format) admins#update
DELETE /admins/:id(.:format) admins#destroy
crons_expirylink_alert GET /crons/expirylink_alert(.:format) crons#expirylink_alert
users_sign_out GET /users/sign_out(.:format) devise/sessions#destroy
root GET / campaigns#latest
After hardwork of 2 days I have finally solved it myself. Every thing was fine except the gem itself. If you just write the gem omniauth-facebook it would install version 1.4.0 which is very old. I update it to gem 'omniauth-facebook', '~> 4.0' and it is working like a charm. So if anyone face the same issue he should update the gem.

Passing nested params from create in redirect to show of nested controller

I have a schema like:
Company belongs to Users (devise)
Quote belongs to Company
Employees belong to Company
I have a simple_form_for using cocoon to create Company & Quote & Employees from the create action in the Company controller, all objects being created just fine by the create method. But on .save of these objects I am trying to redirect to Quotes#show of the quote created by Companies#create but I'm having trouble getting the quote_id over to Quotes#show.
Can you help me understand how to get the right params over to succesfully redirect? Thanks.
companies.rb
class CompaniesController < ApplicationController
before_action :authenticate_user!, only: [ :new, :create, :edit, :update, :destroy ]
def new
#company = Company.new
#company.quotes.build
#company.employees.build
end
def create
#company = current_user.companies.new(company_params)
if #company.save
redirect_to company_quote_url(#company.id), notice: 'Quote request created'
else
render :new
end
end
private
def company_params
params.require(:company).permit(:co_name, :co_number, :postcode, :industry,
:quotes_attributes => [:id, :lives_overseas, :payment_frequency],
:employees_attributes => [:id, :first_name, :last_name, :email, :gender, :date_of_birth, :salary, :_destroy] )
end
end
quotes.rb
class QuotesController < ApplicationController
def show
#quote = #company.quotes.find(params)
end
end
routes
quotes_show GET /quotes/show(.:format) quotes#show
quotes_index GET /quotes/index(.:format) quotes#index
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
user_password PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
POST /users/password(.:format) devise/passwords#create
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
user_registration PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
POST /users(.:format) devise/registrations#create
company_quotes GET /companies/:company_id/quotes(.:format) quotes#index
company_quote GET /companies/:company_id/quotes/:id(.:format) quotes#show
company_employees GET /companies/:company_id/employees(.:format) employees#index
company_employee GET /companies/:company_id/employees/:id(.:format) employees#show
companies GET /companies(.:format) companies#index
POST /companies(.:format) companies#create
new_company GET /companies/new(.:format) companies#new
edit_company GET /companies/:id/edit(.:format) companies#edit
company GET /companies/:id(.:format) companies#show
PATCH /companies/:id(.:format) companies#update
PUT /companies/:id(.:format) companies#update
DELETE /companies/:id(.:format) companies#destroy
root GET / companies#new
error message
No route matches {:action=>"show", :company_id=>65, :controller=>"quotes"} missing required keys: [:id]
I can't see how to get the quote_id route accross from Companies#create over to Quotes#show. When I run the redirect like; redirect_to company_quote_url(company_params) the error shows the header params being passed like this, i.e. this is what's available;
No route matches {:action=>"show", "co_name"=>"acme1", "co_number"=>"12345678", :controller=>"quotes",
"employees_attributes"=>{"0"=>{"first_name"=>"brian", "last_name"=>"blessed", "email"=>"brian#test.com", "gender"=>"m", "date_of_birth(1i)"=>"2001", "date_of_birth(2i)"=>"6", "date_of_birth(3i)"=>"28", "salary"=>"10000", "_destroy"=>"false"}}, "industry"=>"financial_services", "postcode"=>"al8 8ba",
"quotes_attributes"=>{"0"=>{"lives_overseas"=>"true", "payment_frequency"=>"annually"}}} missing required keys: [:company_id, :id]
I've played and failed with different variations of;
(params[:company][:quote_attributes[:id]])
(#company.quote.id)
yet i just can't seem to get it right, can anyone help please. Thanks
Since company has_many quotes, you should track the latest quote for that company and redirect to that show view of the quote.
def create
#company = current_user.companies.new(company_params)
if #company.save
#quote = #company.quotes.last
redirect_to company_quote_url(#company,#quote), notice: 'Quote request created'
else
render :new
end
end
last will give you the latest record with the help of created_at
Also you should tweak your quotes#show like below else it will error out.
def show
#company = Company.find(params[:company_id])
#quote = #company.quotes.find(params[:id])
end

undefined method `user_url' for Devise SessionsController:create

I have user model for authorization with devise gem. I want to add after_sign_in_path method:
# application_controller.rb
protected
# redirecting to appropriate url based on role
def after_sign_in_path_for(resource)
if current_user.has_role?(:admin)
dashboard_path
elsif current_user.has_role?(:student)
root_path
end
end
Whenever I try to sign in I get this error:
undefined method `user_url' for #<Devise::SessionsController:0x007fb89b5b00a8> Did you mean? course_url
I don't know why it says 'did you mean? course_url. But I have course model. And here are my routes:
authenticate :user do
resources :feeds, only: [:index]
resources :courses, only: [:index, :show]
# etc...
end
Also here is the code it points me:
if options.empty?
recipient.send(method, *args)
else
recipient.send(method, *args, options)
end
and first line of log:
actionpack (4.2.4) lib/action_dispatch/routing/polymorphic_routes.rb:220:in `polymorphic_method'
Whenever I commend after_sign_in_path_for I am able to sign in. If I comment contents of after_sign_in_path_for but leave empty after_sign_in_path_for method, I also get this error.
EDIT: I tested that I am not also signed in, not just not redirected. I think error happens right in the call after_sign_in_path_for, not in the redirect_to or whatever. Probably it has to do something with resource.
EDIT2: here are my rake routes:
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PATCH /users(.:format) registrations#update
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
user_confirmation POST /users/confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /users/confirmation/new(.:format) devise/confirmations#new
GET /users/confirmation(.:format) devise/confirmations#show
admin_root GET / rails_admin/main#dashboard
student_root GET / feeds#index
feeds GET /feeds(.:format) feeds#index
courses GET /courses(.:format) courses#index
course GET /courses/:id(.:format) courses#show
schools GET /schools(.:format) schools#index
school GET /schools/:id(.:format) schools#show
universities GET /universities(.:format) universities#index
university GET /universities/:id(.:format) universities#show
rails_admin /admin RailsAdmin::Engine
POST /graphql(.:format) graphql#create
landing_confirmation GET /landing/confirmation(.:format) landing#confirmation
landing_access_denied GET /landing/access_denied(.:format) landing#access_denied
root GET / landing#index
EDIT3: here is my github repo:
https://github.com/yerassyl/nurate
I had the same issue (Rails 7). I fixed it this way:
Add :turbo_stream as a navigational format. This line goes in config/initializers/devise.rb.
config.navigational_formats = ['*/*', :html, :turbo_stream]
Devise issue github
This error is thrown when the after_sign_in_path_for method is redefined in your app and returns nil value. As per my best guest, the provided snippet
def after_sign_in_path_for(resource)
if current_user.has_role?(:admin)
dashboard_path
elsif current_user.has_role?(:student)
root_path
end
end
is giving an error because none of the conditions are getting satisfied and hence the returned value is nil. To avoid such a case, you can always add an else condition which would get satisfied if no other does. Hence the below snippet should have worked (and will work for other users)
def after_sign_in_path_for(resource)
if current_user.has_role?(:admin)
dashboard_path
elsif current_user.has_role?(:student)
root_path
else
some_other_path || root_path
end
end
Hope this helps someone. Cheers :)
In config/devise.rb include this line:
config.navigational_formats = ['/', :html, :turbo_stream]
before rails s
This has done the trick for me.
def after_sign_in_path_for(resource)
if current_user.has_role?(:admin)
dashboard_path
elsif current_user.has_role?(:student)
root_path
else
root_path
end
end
Try to add a else condition in your code. This worked for me. I missed something like this.
If you have the same error with Devise::RegistrationsController, the answers of Jishnu and antoniolulee also works fine.
You have to uncomment in config/initializers/devise.rb line says
config.navigational_formats = ['*/*', :html]
and add :turbo_stream like this
config.navigational_formats = ['*/*', :html, :turbo_stream]
Make sure you've got a method on your AplicationController like follows
def after_sign_in_path_for(resource)
resource.next_step
end
Next_step is an attribute stored in the record during its creation, you may decide to hard code here some other path.

Why users/passwords/new is redirect to root in Rails4?

I'm now using Devise gem in a Ruby application.
It seems a password method doesn't work because in somehow, devise controller is a bit modified. When I typed /users/password/new, it goes back to a root.
I followed a tutorial but now I'm stuck at this really important method that I have to provide to the people.
The passwords_controller.rb file is located in controllers/users/password_controller.rb.
And the code is
class Users::PasswordsController < Devise::PasswordsController
prepend_before_filter :require_no_authentication
# Render the #edit only if coming from a reset password email link
append_before_filter :assert_reset_token_passed, only: :edit
# GET /resource/password/new
def new
self.resource = resource_class.new
end
# POST /resource/password
def create
self.resource = resource_class.send_reset_password_instructions(resource_params)
yield resource if block_given?
if successfully_sent?(resource)
respond_with({}, location: after_sending_reset_password_instructions_path_for(resource_name))
else
respond_with(resource)
end
end
( which is a typical devise controller content )
My user.rb model goes as follow.
...
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:google_oauth2] #, :confirmable
...
I have view files in this directory.
views/users/passwords/create.html.erb, edit.html.erb, new.html.erb
views/users/sessions/new.html.erb
views/users/registrations/edit.html.erb, create.html.erb
Routes file goes like this
devise_for :users, controllers: { passwords: "users/passwords", sessions: "users/sessions", registrations: "users/registrations", omniauth_callbacks: "users/omniauth_callbacks" }
Whenever I type "http://localhost:3000/users/password/new", it goes back to root.
This is my route file.
new_user_session GET /users/sign_in(.:format) users/sessions#new
user_session POST /users/sign_in(.:format) users/sessions#create
destroy_user_session GET /users/sign_out(.:format) users/sessions#destroy
user_omniauth_authorize GET|POST /users/auth/:provider(.:format) users/omniauth_callbacks#passthru {:provider=>/google_oauth2/}
user_omniauth_callback GET|POST /users/auth/:action/callback(.:format) users/omniauth_callbacks#:action
user_password POST /users/password(.:format) users/passwords#create
new_user_password GET /users/password/new(.:format) users/passwords#new
edit_user_password GET /users/password/edit(.:format) users/passwords#edit
PATCH /users/password(.:format) users/passwords#update
PUT /users/password(.:format) users/passwords#update
cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel
user_registration POST /users(.:format) users/registrations#create
new_user_registration GET /users/sign_up(.:format) users/registrations#new
edit_user_registration GET /users/edit(.:format) users/registrations#edit
PATCH /users(.:format) users/registrations#update
PUT /users(.:format) users/registrations#update
DELETE /users(.:format) users/registrations#destroy
POST|GET /:controller(/:action(/:id))(.:format) :controller#:action
root GET / landings#index
The log I got when I go to localhost:3000/users/password/new goes like this.
Started GET "/users/password/new" for ::1 at 2015-10-06 04:11:58 +0900 Processing by Users::PasswordsController#new as HTML Redirected to localhost:3000 Filter chain halted as :is_login rendered or redirected Completed 302 Found in 2ms (ActiveRecord: 0.0ms) This is a log that I got right after I posted the address
If you guys have any hint, please let me know!!
Best
I fix it by append /users/password/edit?reset_password_token=aaaaaa
maybe it is the devise bug .

Resources