Rails 4:"错误的参数数量(2个用于1)"

时间:2015-09-15 17:02:25

标签: ruby-on-rails ruby-on-rails-4 model-view-controller actionmailer

我知道这是一个非常标准的错误,但我无法从其他问题中找出解决方案。

我正在关注 Creating a Scoped Invitation System for Rails

的coderwall教程

我有四个模型,如下:

class User < ActiveRecord::Base
  has_many :administrations
  has_many :calendars, through: :administrations
  has_many :invitations, :class_name => "Invite", :foreign_key => 'recipient_id'
  has_many :sent_invites, :class_name => "Invite", :foreign_key => 'sender_id'
end

class Calendar < ActiveRecord::Base
  has_many :administrations
  has_many :users, through: :administrations
  has_many :invites
end

class Administration < ActiveRecord::Base
  belongs_to :user
  belongs_to :calendar
end

class Invite < ActiveRecord::Base
  belongs_to :calendar
  belongs_to :sender, :class_name => 'User'
  belongs_to :recipient, :class_name => 'User'
end

这是Invite模型的迁移:

class CreateInvites < ActiveRecord::Migration
  def change
    create_table :invites do |t|
      t.string :email 
      t.integer :calendar_id
      t.integer :sender_id
      t.integer :recipient_id
      t.string :recipient_role
      t.string :token
      t.timestamps null: false
    end
  end
end

Invite模型的目标是允许User邀请其他User加入特定Calendar

create Invite表格已嵌入Calendar edit视图中,如下所示:

<h2>Edit <%= @calendar.name %> calendar</h2>

<%= render 'form' %>

<h2>Invite new users to <%= @calendar.name %> calendar</h2>

<%= form_for @invite , :url => invites_path do |f| %>
    <%= f.hidden_field :calendar_id, :value => @invite.calendar_id %>
    <%= f.label :email %>
    <%= f.email_field :email %>
    <%= f.label "Role" %>
    <%= f.radio_button(:recipient_role, "Editor") %>
    <%= f.label "Editor" %>
    <%= f.radio_button(:recipient_role, "Viewer") %>
    <%= f.label "Viewer" %>
    <%= f.submit 'Send' %>
<% end %>

<%= link_to 'Show', calendar_path %> |
<%= link_to 'Back', calendars_path %>

以下是相应的Calendars#Edit

def edit
  @user = current_user
  @invite = @calendar.invites.build
  authorize @calendar
end

这是InvitesController

class InvitesController < ApplicationController
  def create
    @invite = Invite.new(invite_params) # Make a new Invite
    @invite.sender_id = current_user.id # set the sender to the current user
    if @invite.save
      InviteMailer.invite(@invite, new_user_registration_path(:invite_token => @invite.token)).deliver #send the invite data to our mailer to deliver the email
    else
      format.html { render :edit, notice: 'Invitation could not be sent.' }
    end
  end

  private

  def invite_params
    params.require(:invite).permit(:email)
  end

end

最后但并非最不重要的是,这是InviteMailer

class InviteMailer < ApplicationMailer

  def invite(invite)
    @link = new_user_registration_path invite_token: invite.token
    mail to: invite.email, subject: "Calendy Invitation"
  end

end

当我访问http://localhost:3000/calendars/3/edit并提交Invite create表单时,我收到以下错误:

ArgumentError in InvitesController#create
wrong number of arguments (2 for 1)

class InviteMailer < ApplicationMailer

  def invite(invite)
    @link = new_user_registration_path invite_token: invite.token
    mail to: invite.email, subject: "Calendy Invitation"
  end

我的直觉是取代:

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

使用:

InviteMailer.invite(@invite).deliver

但我不确定这是否是正确的解决方案。

有关如何解决此错误的想法吗?

2 个答案:

答案 0 :(得分:2)

可能正在更改invite以允许像下面这样的额外参数也可以正常工作

class InviteMailer < ApplicationMailer
  def invite(invite, link)
    @link = link
    mail to: invite.email, subject: "Calendy Invitation"
  end
end

答案 1 :(得分:1)

  

我的直觉是取代:

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

使用:

InviteMailer.invite(@invite).deliver

是的,没关系,因为你做同样的事情 - 你的InviteMailer#invite将这些数据存储在@link变量中:

class InviteMailer < ApplicationMailer

  def invite(invite)
    @link = new_user_registration_path invite_token: invite.token
    mail to: invite.email, subject: "Calendy Invitation"
  end
end