Ruby on rails 未定义的局部变量或方法“收件人”电子邮件';

Ruby on rails 未定义的局部变量或方法“收件人”电子邮件';,ruby-on-rails,ruby,flash,Ruby On Rails,Ruby,Flash,我试图从我的模型中强制使用flash方法,这样我就可以显示比标准rails错误更好的东西 我在我的模型中有这个方法,invitation.rb: def recipient_is_not_registered if User.find_by_email(recipient_email) false else true end end 我在创建之前用调用它:收件人未注册回调,如果收件人电子邮件已在数据库中注册为用户,则返回false。这将触发if@invitation

我试图从我的模型中强制使用flash方法,这样我就可以显示比标准rails错误更好的东西

我在我的模型中有这个方法,
invitation.rb

def recipient_is_not_registered
  if User.find_by_email(recipient_email)
    false
  else
    true
  end
end
我在创建之前用
调用它:收件人未注册
回调,如果
收件人电子邮件已在数据库中注册为用户,则返回false。这将触发
if@invitation.save
为false,它将沿着
else
分支,显示flash消息

在我的
邀请函\u controller.rb
中,我有:

def create
  @invitation = Invitation.new(invitation_params)
  @invitation.sender = current_user
  if @invitation.save
    redirect_to root_url, notice: 'Invitation was successfully created.'
  else
    flash[:notice] = "The email address #{recipient_email} has already been registered."
  end
end
这给了我前面提到的错误:
未定义的局部变量或方法``recipient\u email'

我尝试了多次邀请。收件人发送电子邮件
,但都没有成功

有两个问题

  • 解决名称错误
  • 了解为什么没有显示闪光灯
  • 您可以尝试以下方法:

    def create
      @invitation = Invitation.new(invitation_params)
      @invitation.sender = current_user
      if @invitation.save
        redirect_to root_url, notice: 'Invitation was successfully created.'
      else
        flash[:notice] = "The email address #{@invitation.recipient_email} has already been registered."
      end
    end
    
    我希望这对您有所帮助。

    您可以尝试以下方法:

    def create
      @invitation = Invitation.new(invitation_params)
      @invitation.sender = current_user
      if @invitation.save
        redirect_to root_url, notice: 'Invitation was successfully created.'
      else
        flash[:notice] = "The email address #{@invitation.recipient_email} has already been registered."
      end
    end
    

    我希望这能对您有所帮助。

    根据您提供的信息,似乎
    收件人\u电子邮件
    邀请
    的一个属性,它将仅在
    邀请
    中可用

    请尝试以下操作:

    def create
      @invitation = Invitation.new(invitation_params)
      @invitation.sender = current_user
    
      if @invitation.save
        redirect_to root_url, notice: 'Invitation was successfully created.'
      else
        flash[:notice] = "The email address #{@invitation.recipient_email} has already been registered."
      end
    end
    

    根据您提供的信息,看起来
    收件人\u电子邮件
    邀请
    的一个属性,它将仅在
    邀请
    中可用

    请尝试以下操作:

    def create
      @invitation = Invitation.new(invitation_params)
      @invitation.sender = current_user
    
      if @invitation.save
        redirect_to root_url, notice: 'Invitation was successfully created.'
      else
        flash[:notice] = "The email address #{@invitation.recipient_email} has already been registered."
      end
    end
    

    我尝试了多次邀请。收件人电子邮件无效。
    请您在问题中包括这些。什么是
    收件人电子邮件
    ,它的定义在哪里?收件人电子邮件
    是属性还是方法?您也可以包括您的架构文件。看起来您正在尝试访问未在模型中定义的参数(收件人电子邮件)。收件人\u电子邮件是您的邀请SQL表中的一列吗?
    flash[:notice]
    将用于重定向。如果您是从
    其他
    条件下呈现模板,请使用
    flash[:now]
    我尝试了多次邀请收件人电子邮件,但都没有效果。
    请您在问题中包括这些。什么是
    收件人电子邮件
    以及它在哪里定义?收件人电子邮件
    是属性还是方法?您也可以包括您的架构文件。看起来您正在尝试访问未在模型中定义的参数(收件人电子邮件)。收件人\u电子邮件是您的邀请SQL表中的一列吗?
    flash[:notice]
    将用于重定向。如果您是从
    else
    条件呈现模板,请使用
    flash[:now]
    为我提供“邀请未定义方法'email'。尝试使用@Invitation.recipient_电子邮件。让我知道这一点。这将为我提供邀请未定义方法'email'。尝试使用@Invitation.recipient#email。让我知道这一点。投票是因为答案正确,但@Muhamad Akbar Bin Widayat最先到达。投票是因为答案正确,但@Muhamad Akbar Bin Widayat最先到达。