Rails 4: "wrong number of arguments (2 for 1)"
Rails 4: "wrong number of arguments (2 for 1)"
我知道这是一个非常标准的错误,但我无法从其他问题中找到针对此特定解决方案的解决方案。
我正在关注这个关于 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
但我不确定这是否真的是正确的解决方案。
知道如何解决这个错误吗?
可能正在更改 invite
以允许像下面这样的额外参数也应该起作用
class InviteMailer < ApplicationMailer
def invite(invite, link)
@link = link
mail to: invite.email, subject: "Calendy Invitation"
end
end
My instinct would be to replace:
InviteMailer.invite(@invite, new_user_registration_path(:invite_token => @invite.token)).deliver
With:
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
我知道这是一个非常标准的错误,但我无法从其他问题中找到针对此特定解决方案的解决方案。
我正在关注这个关于 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
但我不确定这是否真的是正确的解决方案。
知道如何解决这个错误吗?
可能正在更改 invite
以允许像下面这样的额外参数也应该起作用
class InviteMailer < ApplicationMailer
def invite(invite, link)
@link = link
mail to: invite.email, subject: "Calendy Invitation"
end
end
My instinct would be to replace:
InviteMailer.invite(@invite, new_user_registration_path(:invite_token => @invite.token)).deliver
With:
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