我知道这是一个相当标准的错误,但我无法从其他问题中找到这个特定解决方案的解决方案。
我正在学习关于https://coderwall.com/p/rqjjca/creating-a-scoped-invitation-system-for-rails的代码墙教程。
我有四种型号,如下:
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
endInvite模型的目标是允许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但我不知道这是否正确的解决办法。
知道如何修复这个错误吗?
发布于 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变量中:
class InviteMailer < ApplicationMailer
def invite(invite)
@link = new_user_registration_path invite_token: invite.token
mail to: invite.email, subject: "Calendy Invitation"
end
end发布于 2015-09-15 17:11:53
可能会更改invite以允许像下面这样的额外参数也会起作用
class InviteMailer < ApplicationMailer
def invite(invite, link)
@link = link
mail to: invite.email, subject: "Calendy Invitation"
end
endhttps://stackoverflow.com/questions/32591713
复制相似问题