Ruby on rails Rails 4-参数错误-作用域邀请

Ruby on rails Rails 4-参数错误-作用域邀请,ruby-on-rails,ruby,methods,arguments,Ruby On Rails,Ruby,Methods,Arguments,我试图找出如何遵循本教程中的想法: 我被困在如何邀请用户的问题上。我有一个表单可以邀请在show页面上工作的用户。它一直工作到我按send的时候。然后我得到一个错误,它说: wrong number of arguments (given 1, expected 0) 我不知道什么是争论。我不确定被称为给定参数的东西是什么,我不知道系统期望什么。我一直在试图弄清楚什么是争论,以及我是否需要弄清楚如何提高期望值或降低被计算在内的任何东西 我发现这篇文章试图解释什么是参数:但在所有给出的例子中,

我试图找出如何遵循本教程中的想法:

我被困在如何邀请用户的问题上。我有一个表单可以邀请在show页面上工作的用户。它一直工作到我按send的时候。然后我得到一个错误,它说:

wrong number of arguments (given 1, expected 0)
我不知道什么是争论。我不确定被称为给定参数的东西是什么,我不知道系统期望什么。我一直在试图弄清楚什么是争论,以及我是否需要弄清楚如何提高期望值或降低被计算在内的任何东西

我发现这篇文章试图解释什么是参数:但在所有给出的例子中,它没有显示没有参数的方法,也没有实际说明参数的作用。我还是很困惑

错误消息中的引用是:

def existing_user_invite
    mail(
      :subject => "You've been invited to join a team",
      :to  => 'hello@you.com',
      :from => 'me@you.com',
      :html_body => ''
      # :track_opens => 'true'
      )
  end
它还引用了我的创建操作中的这一行:

def create
      @invite = Invite.new(invite_params)
      @invite.sender_id = current_user.profile.id
      if @invite.save
           InviteMailer.existing_user_invite(@invite).deliver 

           @invite.recipient.project.push(@invite.project)
        else
            #send new user email invitation to join as a user and then as part of this team
           @invite.recipient.project.push(@invite.project)

           # InviteMailer.new_user_invite(@invite, new_user_registration_path(:invite_token => @invite.token)).deliver
        end

         # oh no, creating an new invitation failed

    end
教程创建方法显示了以下内容:

def create
  @invite = Invite.new(invite_params)
  @invite.sender_id = current_user.id
  if @invite.save

    #if the user already exists
    if @invite.recipient != nil 

       #send a notification email
       InviteMailer.existing_user_invite(@invite).deliver 

       #Add the user to the user group
       @invite.recipient.user_groups.push(@invite.user_group)
    else
       InviteMailer.new_user_invite(@invite, new_user_registration_path(:invite_token => @invite.token)).deliver
    end
  else
     # oh no, creating an new invitation failed
  end
end
我发现通常很难找到解释组件是什么的rails或ruby资源。我将我之前的问题固定在一起,以提供有关我试图解决的问题的更多背景信息:

邀请_mailer.rb

class InviteMailer < ActionMailer::Base
  # include Devise::Mailers::Helpers


  def existing_user_invite(invite)
    mail(
      :subject => "You've been invited to join a research project team",
      :to  => 'hello@you.com',
      :from => 'me@you.com',
      :html_body => ''
      # :track_opens => 'true'
      )
  end
end
classinvitemailer“您已被邀请加入一个研究项目团队”,
:to=>'hello@you.com',
:from=>'me@you.com',
:html_body=>“”
#:track_opens=>“true”
)
结束
结束
参数数目错误(给定1,应为0)

您有
existing\u user\u invite
,它不需要任何参数,但您正在传递一个(
existing\u user\u invite(@invite)
)。如果要将参数传递给该方法,则应将其更改为允许参数

def existing_user_invite(invite)
  mail(
  :subject => "You've been invited to join a team", 
  :to  => 'hello@you.com', 
  :from => 'me@you.com', 
  :html_body => '' 
  # :track_opens => 'true'
  )
end

@Pavan的回答解释了为什么会出现第一个错误

参数数目错误(给定1,应为0)

第二个问题是,为什么堆栈级别太深

我猜
现有的\u user\u invite
方法在错误的类中。您可能在
Invite
模型中拥有它,但它应该真正转到
InviteMailer

所以试试看

#app/mailers/invite_mailer.rb
class InviteMailer < ApplicationMailer

  # there may be other methods in this class too 

  def existing_user_invite(invite)
     mail(
       :subject => "You've been invited to join a team", 
       :to  => 'hello@you.com', 
       :from => 'me@you.com', 
       :html_body => '' 
       # :track_opens => 'true'
      )
  end
end
#app/mailers/invite_mailer.rb
类InviteMailer“您已被邀请加入团队”,
:to=>'hello@you.com', 
:from=>'me@you.com', 
:html_body=>“”
#:track_opens=>“true”
)
结束
结束
请注意,
existing\u user\u invite
方法接受1个参数(invite),该参数是从控制器传递的。(如@Pavan所述)


但是,如果您仍然存在问题,请发布错误完整跟踪,以便更容易找到问题。:)

嗨,Pavan,我试过这个,但它导致了一个错误,说:“堆栈级别太深了”。据我所知,发生这种错误是因为代码卡在一个循环中:要么是一个方法自己调用,要么是两个方法相互重复调用。我不知道这意味着什么,也不知道通过添加一个参数会发生什么。下一步我会努力了解更多。@Mel没想到。能否尝试将参数名称更改为其他名称。。像
def existing\u user\u invite(邀请)
但是(邀请)是什么意思?这只是我应该插入的一个随机单词吗?我尝试过(邀请),但它给出了相同的错误。不过,我对这个建议更感到困惑——如果我可以把任何单词作为参数输入——如果我没有单词,为什么它会抱怨?@mel如果您声明一个不应该接收任何参数的方法,并通过传递参数来调用它——这就是您将要收到的错误。关于参数的命名-这根本不重要,因为参数将是方法范围内的变量。虽然如果你在同一个类中有一个与你的参数同名的方法,那么最好用不同的名称来命名Hi Sam,非常感谢你的关注。但是现有的用户invite(invite)在invite\u mailer.rb文件中。