我的userinfos控制器处理用户信息。一旦用户注册,我希望他们转到页面,让他们填写用户信息,如姓名,电子邮件,gpa,大学...让我们说他们在填写表格之前离开,当他们再次登录,我想检查用户信息是否已填写,如果没有,我想带他们进入新的信息页面。我该怎么做才能实现这一目标?我被告知要将此代码添加到应用程序
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def after_sign_in_path_for(resource)
if @userinformation.Userinfo.count.zero?
new_userinfo_path
else
root_path
end
end
def after_sign_up_path_for(resource)
if @userinformation.Userinfo.count.zero?
new_userinfo_path
else
root_path
end
end
end
但它不起作用。自&#34; @ userinformation&#34; userinfos控制器中的实例变量,它与应用程序控制器无关,它不起作用。我知道上面的控制器中的代码是错误的,但这是我想要的逻辑。如果特定用户的Userinfo模型中的数据计数为0,那么我想将它们引导到&#34; new_userinfo_path&#34;。
我的Userinfo控制器:
class UserinfosController < ApplicationController
before_action :find_userinfo, only: [:show, :edit, :update, :destroy]
def index
@userinfors = Userinfo.all
@myvideo = Video.all
end
def show
@myvideo = @userinformation.videos.last
end
def new
@userinformation = current_user.userinfos.build
end
def create
@userinformation = current_user.userinfos.build(userinfo_params)
if @userinformation.save
redirect_to userinfo_path(@userinformation)
else
render 'new'
end
end
def edit
end
def update
if @userinformation.update(userinfo_params)
redirect_to userinfo_path(@userinformation)
else
render 'edit'
end
end
def destroy
@userinformation.destroy
redirect_to root_path
end
private
def userinfo_params
params.require(:userinfo).permit(:name, :email, :college, :gpa, :major)
end
def find_userinfo
@userinformation = Userinfo.find(params[:id])
end
end
Userinfo模型:
class Userinfo < ActiveRecord::Base
belongs_to :user
has_many :videos, through: :user
def info_complete?
name? && email? && college? && gpa? && major?
end
end
用户模型:
class User < ActiveRecord::Base
has_many :userinfos
has_many :videos
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
请告诉我你们是否需要更多信息来更好地理解这个问题。
答案 0 :(得分:4)
所以我要做的不是尝试计算实例对象,而是转到userInfo模型并创建一个方法来检查它。会是这样的:
def info_complete?
name? && email? && college? && gpa? && major?
end
所以现在你有一个方法会返回false,除非userInfo中的所有字段都有一个值(.e.g表格不完整,我们应该在那里重定向)。
现在我们可以回到after_sign_in_path
方法并查询resource
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def after_sign_in_path_for(resource)
# I'm not too sure how userInfo relates to a user in your app
if resource.userinfo.info_complete?
redirect_to root_path
else
redirect_to new_userinfo_path
end
end
end
希望这有帮助。
答案 1 :(得分:1)
使用User和Userinfo之间的关联来检查用户是否有user_infos
。
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def after_sign_in_path_for(resource)
if resource.userinfos.empty?
new_userinfo_path
else
root_path
end
end
end
答案 2 :(得分:0)
为什么不简单地在SignUpController
或SignInController
操作中重定向用户?
class SignUpController < ApplicationController
// ...
def create
// Sign up... the user probably won't have any userinfos yet.
redirect_to new_userinfo_path
end
// ...
end
class SignInController < ApplicationController
// ...
def create
// Sign up... the user probably won't have any userinfos yet.
if @userinformation.userinfo.count.zero?
redirect_to new_userinfo_path
else
redirect_to root_path
end
end
// ...
end