我是ror的新手 . 我已经使用设计创建了一个表单..注册表单正在运行..值也保存在db中但保存后重定向到用户页面...有没有办法改变它.. .i希望在用户提交表单后链接另一个表单...
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
结束
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
结束
需要重定向到另一个控制器动作...你可以提出任何想法来解决它 .
3 回答
您只需在那里指定路径名称 . 更改:
至:
阅读docs:
Yon可以覆盖你的设计注册控制器
在这种方法中,您只需编写逻辑
或者,在保存资源后,您可以在注册控制器创建方法中执行一项操作
1. Make a new controller "registrations_controller.rb" and customize the appropriate method:
If the account that is registered is confirmable and not active yet, you have to override after_inactive_sign_up_path_for method.
2. Modify config/routes.rb to use the new controller Modify your devise_for line in routes.rb to look like this.
Optionally Copy Views
注意:在运行Ruby 1.9.2-p290的rails 3.2.5中,似乎没有必要执行以下步骤 . 您只需创建RegistrationsController并更改路线即可 . 然后,通过继承自Devise :: RegistrationsController,您可以获取现有的Devise注册视图 . 无论您是否已经创建了这些视图,这都成立
或不 .
注意:更改config / routes.rb文件中的控制器后,您需要将设计注册视图复制到新的app / views / registrations路径 .
使用“rails generate devise:views”复制文件 . 执行此操作后,您需要将views / devise / registrations / new.html.erb复制到views / registrations / new.html.erb否则当您转到users / sign_up时会出现“Missing Template”错误
3. Modify config/application.rb