Rails 在设计中注册后创建个人资料

Rails create profile after sign up in devise

我已经在我的应用程序中安装了设计,现在我想在个人(用户)注册后立即创建个人资料并将他们重定向到个人资料页面

这是我的个人模型

class Individual < ActiveRecord::Base
  has_one :profile
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

我的个人资料模型是

  class Profile < ActiveRecord::Base
      belongs_to :individual
      before_create :build_profile
 def completed_profile?
    self.first_name.present? && self.last_name.present? && self.address_line_1.present? && self.city.present? && self.state.present? && self.zipcode.present?
  end
    end

配置文件的迁移文件是

class CreateProfiles < ActiveRecord::Migration
  def change
    create_table :profiles do |t|
      t.belongs_to :individual, index: true
      t.string :first_name
      t.string :last_name
      t.string :age
      t.string :birth_date
      t.string :gender
      t.string :bio
      t.string :linkedin_profile
      t.string :facebook_profile
      t.string :twitter_profile
      t.integer :mobile_no

      t.timestamps null: false
    end
  end
end

我的配置文件控制器被指定为

class ProfilesController < ApplicationController
    before_action :authenticate_individual!
    before_action :find_profile, only: [:show, :edit, :update, :destroy]

    respond_to :html

    def index
      @profiles = Profile.all
    end

    def new
      @profile = current_individual.build_profile
    end

    def create
      @profile = current_individual.build_profile(profile_params)
      if @profile.save
        flash[:success] = "Profile saved"
        redirect_to current_individual_path
      else
        flash[:error] = "Error"
        render :new
      end
    end

    def show
      @profile = Profile.find(params[:id])
    end

    def edit
    end

    def update
      @profile.update(profile_params)
      respond_with(@profile)
    end


    private

    def find_profile
      @profile = Profile.find(params[:id])
    end

    def profile_params
      params.require(:profile).permit(:first_name, :last_name, :birth_date,
                                      :gender, :bio, :personal_website, :linkedin_profile, :facebook_profile,
                                      :mobile_no, :telephone_no)
    end
  end

我的路线为

devise_for :individuals

我的应用程序控制器有

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  def after_sign_in_path_for(resource)
    if current_individual.completed_profile?
      redirect_to root_path
    else
      redirect_to edit_individual_profile_path
    end
  end
end

请告诉我用户如何才能注册,并且在登录后他们将被重定向到个人资料的编辑视图,个人可以在其中编辑个人资料 谢谢!!

我认为您在保存个人时正在创建一个新的配置文件,因此您可以做的是创建一个名为 completed_profile 的方法。因此,在您的 individual 模型中,您可以做的是创建一个实例方法:

def completed_profile?
  self.first_name.present? && self.last_name.present? && self.address_line_1.present? && self.city.present? && self.state.present? && self.zipcode.present?
end

在你的应用程序控制器中你可以这样定义:

def after_sign_in_path_for(resource)
  redirect_to edit_individual_profile_path unless current_individual.profile.completed_profile?
end

来源:https://github.com/plataformatec/devise/wiki/How-To%3A-Redirect-to-a-specific-page-on-successful-sign-in-and-sign-out

因此,当用户每次 sign_in 时,如果他的个人资料未完成,它将被重定向到编辑个人资料页面。

根据您的需要修改代码这是从我的应用程序中提取的。

希望对您有所帮助。