如何根据rails中输入的信息将用户发送到不同的路径?

我的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 

但它不起作用。 由于“@userinformation”是userinfos控制器中的实例变量,并且它与应用程序控制器无关,因此它不起作用。 我知道上面的控制器中的代码是错误的,但那是我想要的逻辑。 如果特定用户的Userinfo模型中的数据计数为0,那么我想将它们指向“new_userinfo_path”。

我的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 

如果你们需要更多的信息来更好地理解这个问题,请告诉我。

所以我要做的不是尝试计算实例对象,而是转到userInfo模型并创建一个方法来检查它。 会是这样的:

 def info_complete? name? && email? && college? && gpa? && major? end 

所以现在你有一个方法会返回false,除非userInfo中的所有字段都有一个值(.eg表单不完整,我们应该在那里重定向)。

所以现在我们可以回到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 

希望这可以帮助。

使用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 

为什么不简单地在SignUpControllerSignInController操作中重定向用户?

 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