【问题标题】:One Model and two controller validation Ruby on Rails?一个模型和两个控制器验证 Ruby on Rails?
【发布时间】:2016-12-21 20:46:40
【问题描述】:

最让我困惑的一件事是在一个带有两个控制器的模型中进行验证。我有一个登录系统,用于注册和登录用户。两者都使用相同的模型,但都没有使用相同数量的 HTML 小部件。一个控制器包含密码、重新输入密码、用户名、名字和第二个名字等。第二个控制器仅使用用户名和密码字段。对于这种情况,您将如何在同一模型中进行验证?

谢谢

这里是注册新用户的控制器:

class UsersController < ApplicationController
  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)

    if @user.save
      session[:user_id] = @user.id
      redirect_to '/cool'
    else
      @user = Newuser.new
      @user.valid? 
      @user.errors.messages
      render 'new'
    end
  end

  private

  def user_params
    params.require(:user).permit(:name, :email, :password, :salt, :firstname,      :secondname, :address, :postcode)
  end
end

第二个控制器:

class LoginsController < ApplicationController
  before_filter :authorize

  def index
    @rentals = Rental.where(user_id: current_user.id).limit(5)
    @buys = Buy.where(user_id: current_user.id).limit(5)
    @users = User.where(id: current_user.id)
    @buyGames = BuyGame.where(user_id: current_user.id).limit(5)
  end 

  def destroy
    @user = User.find(params[:id])
    @user.destroy
    redirect_to '/logout'
  end

  def edit
    @user = User.find(params[:id])
  end

  def update
    @user = User.find(params[:id])

    if @user.update(account_params)
      redirect_to '/cool'
    else
      render 'edit'
    end
  end

  private

  def account_params
    params.require(:user).permit(:name, :email, :password, :salt, :firstname, :secondname, :address, :postcode)
  end
end

这是我的模型:

class User < ApplicationRecord
  has_secure_password
end

【问题讨论】:

  • 请格式化问题,阅读这样格式化的代码很痛苦
  • 对不起,伙计,没看到 :)。
  • 那里,我为你做了;)
  • 奇怪的登录控制器会破坏/更新用户
  • 我的理解是,当用户注册和用户登录时,您需要在两种可能的情况下进行验证。当用户注册时,还有其他可用字段,例如名字、名字、重新输入密码。在第二种情况下,此值不可用。所以我会在他们验证之前检查他们的存在。

标签: ruby-on-rails


【解决方案1】:

一种方法是从模型中删除验证并将它们放入form objects。对于这种情况,您将有两个表单对象,每个对象都有自己的一组验证。并且您在各自的控制器中使用适当的控制器。大致如下:

# logins_controller
def update
  login_form = FormObjects::LoginForm.new(login_params)
  if login_form.valid?
    redirect_to '/cool'
  else
    render 'edit'
  end  
end

# users controller
def create
  signup_form = FormObjects::SignupForm.new(user_params)
  if signup_form.save
    redirect_to '/cool'
  else
    render 'new'
  end
end

# signup_form
module FormObjects
  class SignupForm
    include ::ActiveMode::Model

    validate_presense_of :email, :password, :password_confirmation, :address, :whatever_else

    def save
      # create user here
    end
  end
end

# login_form
module FormObjects
  class LoginForm
    include ::ActiveMode::Model

    validate_presense_of :email, :password
  end
end

【讨论】:

    【解决方案2】:

    您可以简单地指定对操作的验证,即:

    validates :first_name, presence: true, on: :create # 在更新或任何其他操作时不会验证名字的存在

    【讨论】:

      【解决方案3】:

      我相信您正在寻找的技巧是定义validation actions on create/update of the model。大致是这样的:

      class User < ActiveRecord::Base
        # These are example validations only; replace with your actual rules.
        validates :password, confirmation: true
        validates_presence_of :username
        validates :first_name, presence: true, format: {with: /.../}, on: create
        validates :last_name, presence: true, format: {with: /.../}, on: create
      end
      

      ...但是,我不清楚您为什么要在您的特定示例中执行此操作。建议始终对 first_name 等字段运行所有验证检查,以帮助维护数据完整性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-19
        • 2012-09-20
        • 1970-01-01
        • 1970-01-01
        • 2010-10-03
        • 2012-12-31
        • 1970-01-01
        • 2011-01-04
        相关资源
        最近更新 更多