![](/img/trans.png)
[英]Rails Authentification with Devise: Redirect to Sign-In form after Sign-Up
[英]Need to redirect to another form after devise sign up form
我是ror的新手。.我已經使用devise創建了一個表單。.注冊表單也正在工作..這些值也保存在db中,但保存后將重定向到用戶頁面...是否可以更改它。我希望用戶提交表單后再鏈接另一個表單...
控制者
class UserRequestsController < ApplicationController
def new
@user_request = UserRequest.new
end
end
應用程序助手
def resource_name
:user
end
def resource
@resource ||= User.new
end
def devise_mapping
@devise_mapping ||= Devise.mappings[:user]
end
模型
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
應用控制器
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
需要重定向到另一個控制器動作...您能提出任何解決辦法嗎?
您只需要在此處指定路徑名。 更改:
def after_sign_up_path_for(resource)
root to: "vendors#invited_new", as: :manager_root
end
至:
def after_sign_up_path_for(resource)
manager_root_path
end
閱讀文檔 :
def stored_location_for(resource)
nil
end
def after_sign_in_path_for(resource)
# path_to_redirect_to For eg. root_path
end
Yon可以覆蓋您的設備注冊控制器
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
用這種方法,您只需編寫邏輯
或者,您可以在保存資源后在注冊控制器的create方法中做一件事
if resource.save
if resource.active_for_authentication?
if your condition
respond_with resource, location: your_redirect_path
else
end
end
end
1.新建一個控制器“ registrations_controller.rb”,並自定義適當的方法:
class RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
'/an/example/path' # Or :prefix_to_your_route
end
end
如果已注冊的帳戶是可確認的並且尚未激活,則必須重寫after_inactive_sign_up_path_for方法。
class RegistrationsController < Devise::RegistrationsController
protected
def after_inactive_sign_up_path_for(resource)
'/an/example/path' # Or :prefix_to_your_route
end
end
2.修改config / routes.rb以使用新的控制器修改routes.rb中的devise_for行,使其看起來像這樣。
devise_for :users, controllers: { registrations: "registrations" }
(可選)復制視圖
注意:在運行Ruby 1.9.2-p290的rails 3.2.5中,似乎不需要以下步驟。 您只需要創建您的RegistrationsController並更改您的路線即可。 然后,通過從Devise :: RegistrationsController繼承,選擇現有的Devise注冊視圖。 無論您是否已經使用這些視圖創建了這些視圖
rails g devise:views
或不。
注意:在config / routes.rb文件中更改控制器后,您將需要將devise注冊視圖復制到新的app / views / registrations路徑。
使用“ rails generate devise:views”復制文件。 完成此操作后,您需要將views / devise / registrations / new.html.erb復制到views / registrations / new.html.erb,否則在轉到users / sign_up時會出現“缺少模板”錯誤
3.修改config / application.rb
如果在“ / users / sign_up”頁面上遇到“ MissingTemplate”錯誤,則可能需要此行。
config.paths['app/views'] << "app/views/devise"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.