【问题标题】:Ruby: How to reference model variables in a controller?Ruby:如何在控制器中引用模型变量?
【发布时间】:2013-09-21 21:40:56
【问题描述】:

目前,一个用户可以创建多个帐户并为每个帐户使用相同的 paypal_email。

我正在尝试将每个用户限制为一个唯一的 paypal_email。我不想使用 :uniqueness => true 因为在某些情况下管理员可以允许用户拥有多个帐户(因此共享 1 个 paypal_email)。

为了将每个用户限制为 1 个唯一的 paypal_email,我在用户模型中设置了一个名为 :warning 的字段。如果 :warning 设置为“已接受”,则该 paypal_email 的用户模型中不应有重复项。如果 :warning 是“禁止”,那么提交的 paypal_email 应该被拒绝。当 :warning 是“警告”时,它会被忽略。

ReviewPayments 控制器在用户提交他们的 paypal_email 时验证 paypal_email(ReviewPayments 模型中的重复 paypal_email 字段,如果有效,则复制到 Users 模型中)。 ReviewPayment 模型属于Reviews 模型,Reviews 模型属于Users 模型。 (例如,ReviewsPayment

在测试(临时站点)中,当我提交被禁止的 paypal_email(即:warning == "Banned")时,duplicate_paypal?方法返回true而不是banned_pa​​ypal?方法。事实上,当我输入用户数据库中已经存在的任何 paypal_email 时,它会通过 duplicate_paypal 方法被拒绝。如果我输入一个全新的 paypal_email,它就会通过。似乎 .where 条件设置不正确。这意味着我没有正确引用 User 模型。

帮助?

class My::ReviewPaymentsController < ApplicationController
  before_filter :require_login
  before_filter :load_review
  before_filter :new_payment

  def new
    @payment.paypal_email = current_user.paypal_email
  end

    def create
    @payment.payment_type = "PayPal"
    @payment.paypal_email = params[:review_payment][:paypal_email]


    # if paypal_email is not a duplicate then check to ensure
    # it's not a banned user's paypal account
    if :duplicate_paypal?
      @payment.errors.add(:paypal_email, "This Paypal account is associated with another registered user.  
      Please provide your own Paypal account.")
      render :new     
    elsif :banned_paypal?
      @payment.errors.add(:paypal_email, "This Paypal account is disabled.  
      Please provide another Paypal account.")
      render :new
    elsif @payment.save && @review.start_audit
      flash[:success] = "Thank you for your review! You will receive your payment shortly."
      redirect_to root_path
    else
      render :new
    end
  end

  def duplicate_paypal
    @countdupe = 0
    @currentuserid = current_user.reviews.find params[:user_id]
    @countdupe = User.where(:warning == "Accepted" && :id != @currentuserid).find_all_by_paypal_email(current_user.paypal_email).count 
    @countdupe > 0
  end

def banned_paypal
        @countdupe = 0
        @countdupe = User.where(:warning == "Banned").find_all_by_paypal_email(current_user.paypal_email).count 
        @countdupe != 0
      end

  protected

  def load_review
    @review = current_user.reviews.find params[:review_id]
    @manuscript = @review.manuscript
  end

  def new_payment
    @payment = ReviewPayment.new
    @payment.review = @review
    @payment.review_payment_amount = ReviewPayment::RewardAmount
  end
end

【问题讨论】:

    标签: ruby-on-rails ruby rails-activerecord


    【解决方案1】:

    在您的模型中添加验证并查看以下链接:

    http://edgeguides.rubyonrails.org/active_record_validations.html#using-a-string-with-if-and-unless

    这将立即解决您的问题。要了解主要思想:

    class User < ActiveRecord::Base
      validates :paypal_email, uniqueness: true, if: Proc.new { |u| u.warning == "Banned" }
    end
    

    【讨论】:

    • 感谢您的回复。但是如果条件不匹配, :if 选项将不会验证 paypal_email。我希望变量 paypal_email 无论如何都经过验证(目前有 :presence 和 :format 验证器)。此外,paypal_email 验证在 ReviewPayments 类中完成,而不是在 User 类中完成,因为它是 ReviewPayments 中的强制输入,而不是 User。如果提交的 paypal_email 被禁止或重复(仅当 :warning 为“已接受”),我需要一种检查现有用户数据库的方法,并忽略 :warning 为“警告”。
    【解决方案2】:

    这里最好使用条件验证 http://edgeguides.rubyonrails.org/active_record_validations.html#conditional-validation

    在你的模型中你可以这样写:

    validates :paypal_email, uniqueness: true, if: :warning?
    

    并使用警告属性作为布尔值。

    【讨论】:

    • 您应该更新链接 - 您的链接涵盖了 rails 版本 2.3.11 - 这是过时的。我在答案中链接了 edgeguide。
    • @tokenvolt,感谢您的回复。但这不会解决我的问题。你能看到我上面的问题并请帮忙吗?
    • 我仔细阅读了您的问题,但我不明白您为什么需要检查“接受”和“禁止”值的警告?难道只是为了给用户更好的错误反馈?
    • 还有一个问题@currentuserid = current_user.reviews.find params[:user_id] - 根据这段代码,得到了Review模型的实例,但没有得到用户的ID。还是 current_user.reviews 返回用户列表?然而,你不会得到 id。
    • @tokenvolt 为什么当前的userid 不能给我:user_id?不应该使用 find 和 params 从评论模型中提取 current_user 的 id 吗?顺便说一句,我使用 :warning 标签(“接受”、“禁止”、“警告”)来区分我不想重复的黑白贝宝(接受)、不再受欢迎的(禁止)和那些我不想检查是否受骗的人(警告)。
    猜你喜欢
    • 1970-01-01
    • 2011-01-26
    • 2011-02-27
    • 2014-11-14
    • 2020-05-08
    • 1970-01-01
    • 2014-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多