首页 文章

在设计注册表单后需要重定向到另一个表单

提问于
浏览
0

我是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 回答

  • 2

    您只需在那里指定路径名称 . 更改:

    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
    

    阅读docs

    def stored_location_for(resource)
      nil
    end
    
    def after_sign_in_path_for(resource)
      # path_to_redirect_to For eg. root_path
    end
    
  • 0

    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
    

    在这种方法中,您只需编写逻辑

    或者,在保存资源后,您可以在注册控制器创建方法中执行一项操作

    if resource.save
      if resource.active_for_authentication?
        if your condition 
          respond_with resource, location: your_redirect_path
        else
        end
      end
    end
    
  • 1

    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

    注意:在运行Ruby 1.9.2-p290的rails 3.2.5中,似乎没有必要执行以下步骤 . 您只需创建RegistrationsController并更改路线即可 . 然后,通过继承自Devise :: RegistrationsController,您可以获取现有的Devise注册视图 . 无论您是否已经创建了这些视图,这都成立

    rails g devise:views
    

    或不 .

    注意:更改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

    如果在“/ users / sign_up”页面中遇到“MissingTemplate”错误,您可能需要此行 .

    config.paths['app/views'] << "app/views/devise"
    

相关问题