Reputation: 87
i am new to ror..i have created a form using devise..the registeration form is working..the values are saved in db too but after saving it redirects to users page...Is there any way to change it...i want another form to be link once user submits form...
Controller
class UserRequestsController < ApplicationController
def new
@user_request = UserRequest.new
end
end
Application Helper
def resource_name
:user
end
def resource
@resource ||= User.new
end
def devise_mapping
@devise_mapping ||= Devise.mappings[:user]
end
Model
class UserRequest < ActiveRecord::Base
belongs_to :user
validates :user_id, :email, :name, :invitation_type, presence: true
validates :email, uniqueness: true
validates :email, email: true
validate :email_not_in_use_already
validate :invitation_type_is_valid
def email_not_in_use_already
if new_record? && User.where(email: self.email).any?
errors.add(:email, "is already in use")
end
end
def invitation_type_is_valid
unless INVITATION_TYPES.include?(self.invitation_type)
errors.add(:invitation_type, "is not a valid type of invitation")
end
end
end
Application Controller
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) do |u|
u.permit(:first_name, :last_name, :email, :password, :password_confirmation, :time_zone, :terms_of_service)
end
devise_parameter_sanitizer.for(:account_update) do |u|
u.permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password, :time_zone, :terms_of_service)
end
end
def after_sign_in_path_for(resource)
previous_url = session[:previous_url]
# if user has an invite code and isn't set up yet, direct them to the appropriate creation page
if invited_user_needs_profile?(session[:hash_code])
return path_for_invite(session[:hash_code])
end
user = resource # not checking resource type since right now only one is User; add later if needed
# require acceptance of terms of service
unless user.terms_of_service == true
flash[:alert] = "You have not yet accepted the Terms of Service. Please verify your account information and review the Terms of Service."
return edit_user_registration_path
end
# redirect to previous URLs in case user followed a link or bookmark but they were redirected due to needing to log in
unless Rails.env == "test"
# don't redirect to previous url if it's going to the root or users_path, because in those cases we'd rather take the user to their home page
return previous_url if previous_url.present? && previous_url != root_path && previous_url != new_user_registration_path && !(previous_url =~ /\/users\/password/)
end
if user.planner.present?
planner_path(user.planner.id)
elsif user.vendor.present?
vendor_path(user.vendor.id)
else
root_path
end
end
def after_sign_up_path_for(resource)
root to: "vendors#invited_new", as: :manager_root
end
end
Need to redirect to another controller action...can u please give any idea to get it fixed.
Upvotes: 0
Views: 1872
Reputation: 113
1. Make a new controller "registrations_controller.rb" and customize the appropriate method:
class RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
'/an/example/path' # Or :prefix_to_your_route
end
end
If the account that is registered is confirmable and not active yet, you have to override after_inactive_sign_up_path_for method.
class RegistrationsController < Devise::RegistrationsController
protected
def after_inactive_sign_up_path_for(resource)
'/an/example/path' # Or :prefix_to_your_route
end
end
2. Modify config/routes.rb to use the new controller Modify your devise_for line in routes.rb to look like this.
devise_for :users, controllers: { registrations: "registrations" }
Optionally Copy Views
Note: In rails 3.2.5 running Ruby 1.9.2-p290 the steps below don't seem to be necessary. You just need to create your RegistrationsController and alter your routes. Then, by virtue of inheriting from Devise::RegistrationsController, you pick up the existing Devise registrations views. This holds regardless of whether you've created those views already with
rails g devise:views
or not.
Note: After changing the controller in the config/routes.rb file, you will want to copy over the devise registration views over to the new app/views/registrations path.
Copy the files using "rails generate devise:views". After doing this, you need to copy views/devise/registrations/new.html.erb to views/registrations/new.html.erb Otherwise you will get a "Missing Template" error when you go to users/sign_up
3. Modify config/application.rb
You might need this line in case you encounter "MissingTemplate" error in "/users/sign_up" page.
config.paths['app/views'] << "app/views/devise"
Upvotes: 0
Reputation: 22926
You need to specify only the path name there. Change:
def after_sign_up_path_for(resource)
root to: "vendors#invited_new", as: :manager_root
end
to:
def after_sign_up_path_for(resource)
manager_root_path
end
Read the docs:
def stored_location_for(resource)
nil
end
def after_sign_in_path_for(resource)
# path_to_redirect_to For eg. root_path
end
Upvotes: 2
Reputation: 3323
Yon can override your devise registration controller
class RegistrationsController < Devise::RegistrationsController
##this method calls when signup is success
def after_sign_up_path_for(resource)
if put your condition here
your_redirect_path (replace your different controller path here)
else
root_path
end
end
end
In this method you just write your logic
Or you can do one thing in your registrations controller create method after your resource will be saved
if resource.save
if resource.active_for_authentication?
if your condition
respond_with resource, location: your_redirect_path
else
end
end
end
Upvotes: 1