Rails 4 - 参数错误 - 范围邀请

时间:2016-06-16 10:04:05

标签: ruby-on-rails ruby methods arguments

我试图弄清楚如何按照本教程中的想法进行操作:

https://coderwall.com/p/rqjjca/creating-a-scoped-invitation-system-for-rails

我对如何邀请用户感到困惑。我有一个表单来邀请在显示页面上工作的用户。它工作到我按发送的程度。然后我收到一个错误,上面写着:

wrong number of arguments (given 1, expected 0)

我不知道论证是什么。我不确定一件事是什么被称为给定的论点,我不知道系统期望什么。我一直在试图找出一个论点是什么,以及我是否需要试图弄清楚如何提高期望或减少已被计算的数量。

我发现这篇文章试图解释一个论点是什么:http://www.skorks.com/2009/08/method-arguments-in-ruby/但是在给出的所有例子中,它没有显示没有参数的方法,它实际上并没有说明什么这个论点是要做的。我还是很困惑。

错误消息中的引用是:

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

教程create方法显示了这个:

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资源。我固定以前的问题,提供更多有关我试图解决的问题的背景信息:

Rails 4 - Invite team mates to project

Rails 4 - Associations - adding team mates to teams

Invite_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

2 个答案:

答案 0 :(得分:0)

  

错误的参数数量(给定1,预期为0)

你有existing_user_invite 不希望有任何参数,但是你传递了一个( existing_user_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

答案 1 :(得分:0)

@ Pavan的回答解释了为什么你会收到第一个错误

wrong number of arguments (given 1, expected 0)

然后第二个问题是,为什么要获得stack level too deep

我猜是existing_user_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

请注意existing_user_invite方法取1个参数(邀请),从控制器传递。 (正如@Pavan所说)

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