我已在我的应用程序中安装了设计,现在我想在个人(用户)注册并将其重定向到个人资料页面之后创建个人资料
这是我的个人模特
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
请告诉我如何注册用户并在登录后将其重定向到个人资料的编辑视图,个人可以在其中编辑个人资料 谢谢!!
答案 0 :(得分:4)
我认为您在保存个人时正在创建新的个人资料,因此您可以创建名为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
因此,如果用户将sign_in
每次重定向到编辑个人资料页面,如果他的个人资料未完成。
根据您的需要修改代码,这是从我的应用程序中提取的。
希望这有帮助。