【问题标题】:Rails 4 - argument error - scoped invitationRails 4 - 参数错误 - 范围邀请
【发布时间】:2016-10-17 18:47:08
【问题描述】:

我正在尝试弄清楚如何遵循本教程中的想法:

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

教程创建方法显示如下:

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

【问题讨论】:

    标签: ruby-on-rails ruby methods arguments


    【解决方案1】:

    参数数量错误(给定 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
    

    【讨论】:

    • 嗨,Pavan,我试过这个,但它会导致错误提示:“堆栈级别太深”。据我所知,发生该错误是因为代码卡在一个循环中:一个方法调用自身,或者两个方法重复调用对方。我不知道这意味着什么,也不知道添加参数会如何发生。接下来我将尝试了解更多相关信息。
    • @Mel 没想到会这样。您可以尝试将参数名称更改为其他名称吗?例如def existing_user_invite(invitation)
    • 但是(邀请)是什么意思?这只是我应该插入的一个随机词吗?
    • 我试过(邀请) - 但它给出了同样的错误。不过,我对这个建议更加困惑——如果我可以把任何词作为论据——如果我根本没有词,为什么它会抱怨?
    • @mel 如果您声明一个不应接收任何参数的方法,并通过传递参数来调用它 - 这就是您将收到的错误。关于参数的命名 - 这根本不重要,因为参数将是方法范围内的变量。虽然如果您在同一个类中有一个与您的参数同名的方法,最好以不同的方式命名它
    【解决方案2】:

    @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 提到的)

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

    【讨论】:

    • 嗨,Sam,非常感谢您的关注。但是existing_user_invite(invite) 在invite_mailer.rb 文件中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    • 1970-01-01
    相关资源
    最近更新 更多