首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails 4:“错误的参数数(2对1)”

Rails 4:“错误的参数数(2对1)”
EN

Stack Overflow用户
提问于 2015-09-15 17:02:24
回答 2查看 130关注 0票数 0

我知道这是一个相当标准的错误,但我无法从其他问题中找到这个特定解决方案的解决方案。

我正在学习关于https://coderwall.com/p/rqjjca/creating-a-scoped-invitation-system-for-rails的代码墙教程。

我有四种型号,如下:

代码语言:javascript
复制
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模型的迁移:

代码语言:javascript
复制
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视图中,如下所示:

代码语言:javascript
复制
<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

代码语言:javascript
复制
def edit
  @user = current_user
  @invite = @calendar.invites.build
  authorize @calendar
end

下面是InvitesController

代码语言:javascript
复制
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

代码语言:javascript
复制
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表单时,会得到以下错误:

代码语言:javascript
复制
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

我的本能反应是:

代码语言:javascript
复制
InviteMailer.invite(@invite, new_user_registration_path(:invite_token => @invite.token)).deliver

通过以下方式:

代码语言:javascript
复制
InviteMailer.invite(@invite).deliver

但我不知道这是否正确的解决办法。

知道如何修复这个错误吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-09-15 17:12:00

我的本能反应是: InviteMailer.invite(@invite,new_user_registration_path(:invite_token => @invite.token)).deliver 通过以下方式: InviteMailer.invite(@invite).deliver

是的,这很好,因为您也是这样做的--您的InviteMailer#invite将这些数据存储在@link变量中:

代码语言:javascript
复制
class InviteMailer < ApplicationMailer

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

Stack Overflow用户

发布于 2015-09-15 17:11:53

可能会更改invite以允许像下面这样的额外参数也会起作用

代码语言:javascript
复制
class InviteMailer < ApplicationMailer
  def invite(invite, link)
    @link = link
    mail to: invite.email, subject: "Calendy Invitation"
  end
end
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32591713

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档