简体   繁体   English

保存前检查用户输入

[英]Check User Input Before Saving

Good Day All! 大家好!

Edited for better understanding. 编辑以更好地理解。

First model is Inventory and in this model I have Product_Type, Product_Name and User_ID. 第一个模型是库存,在这个模型中,我有Product_Type,Product_Name和User_ID。

Second model I have Users which consist of First_Name, Last_Name and Pin_Number. 第二个模型我有由名字,姓氏和密码组成的用户。

On my Inventories page I have a form for checking out said Product_Type and Product_Name, also a place for a user to put their Pin_Number in. On submit, it will check the Pin_Number they have typed in and validate it in the Users model and if the Pin_Number is correct it will create an entry with said Product_Type, Product_Name and User_ID (which is pulled from Pin_Number that was submitted.) 在我的库存页面上,我有一张表格,用于检出所说的Product_Type和Product_Name,也可以供用户放置其Pin_Number。提交后,它将检查他们键入的Pin_Number并在Users模型中对其进行验证,以及是否Pin_Number是正确的,它将使用所述Product_Type,Product_Name和User_ID(从提交的Pin_Number中提取)创建一个条目。

I am just trying to figure out how to validate that Pin_Number they submitted. 我只是想弄清楚如何验证他们提交的Pin_Number。

Thats why I thought I had to do some kind of validation and an if statement based on that validation. 这就是为什么我认为我必须进行某种验证以及基于该验证的if语句。 Not sure how to go about that. 不知道如何去做。

I hope this clears up any confusion. 我希望这可以消除任何混乱。

I am just trying to figure out how to validate that Pin_Number they submitted. 我只是想弄清楚如何验证他们提交的Pin_Number。

What constitutes a valid pin_number ? 什么是有效的pin_number Just that it allows you to successfully look up a User ? 仅仅是它允许您成功查找User What if a user enters another user's pin_number ? 如果用户输入另一个用户的pin_number怎么pin_number Is that considered 'valid'? 那被认为是“有效的”吗? Something to think about... 需要考虑的事情...

It would be helpful if you would add to your question what your params look like upon form submission. 如果您向问题添加表单提交后的params ,将会很有帮助。 But, we can do some guess work. 但是,我们可以做一些猜测工作。

So, let's assume that params looks something like: 因此,让我们假设参数看起来像:

{..., "inventory"=>{"product_type"=>"foo", "product_name"=>"Bar"}, "pin_number"=>5, ...}

In your controller, you'll probably do something like: 在您的控制器中,您可能会执行以下操作:

if @user = User.find_by(pin_number: params[:pin_number])
  @inventory = Inventory.new(inventory_params)
  @inventory.user = @user
  if @inventory.valid?
    @inventory.save
    # perhaps do some other stuff...
  else 
    # handle the case where the `@inventory` is not valid
  end
else
  # handle the case where the `@user` was not found
end

This assumes you have something like: 假设您有类似以下内容:

private 

  def inventory_params
    params.require(:inventory).permit(:product_type, :product_name)
  end

In your Inventory model, you probably want to do something like (I apologize, I'm not on Rails 5 yet, so some of the syntax may be incorrect): 在您的Inventory模型中,您可能想要做类似的事情(抱歉,我还没有使用Rails 5,因此某些语法可能不正确):

class Inventory < ActiveRecord::Base 

  validates :user_id,
            :product_type,
            :product_name,
            presence: true

  belongs_to :user

end

You probably also want to consider adding an index on User.pin_number if you're going to be doing a lot of finding by it. 如果您要通过它进行大量查找,则可能还需要考虑在User.pin_number上添加索引。

Not sure if I got the question right, but sounds like a custom validator to me. 不知道我是否正确回答了这个问题,但听起来对我来说是一个自定义验证器。

You can learn more about custom validators in the official Rails documentation under " Custom Validators " 您可以在Rails官方文档的“ 自定义验证器 ”下了解有关自定义验证器的更多信息。

Also, consider moving the class for the custom validator you'll build to a concern, which is a great way to make it reusable. 另外,考虑将要构建的自定义验证程序的类移到某个关注点,这是使其可重用的好方法。 You can find more information on this StackOverflow question and this nice tutorial . 您可以找到有关StackOverflow问题本教程的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM