我使用了以下创建作用域invite (https://coderwall.com/p/rqjjca/creating-a-scoped-invitation-system-for-rails)的教程来创建invite、user、group,但是遇到了上面的错误,并且不确定如何解决它。请记住,我还在学习RoR。谢谢。
我在invites_controller.rb中有以下代码
class InvitesController < ApplicationController
def new
@invite = Invite.new
end
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.new_user_invite(@invite, new_user_registration_path(:invite_token => @invite.token)).deliver #send the invite data to our mailer to deliver the email
flash[:notice] = "Thanks, invitation has successfully been sent"
redirect_to user_groups_url
else
flash[:notice] = "oh no, creating an new invitation failed"
redirect_to invites_path
end
end
private
def invite_params
params.require(:invite).permit(:email, :recipient_id, :user_group_id, :sender_id)
end
end
invite to send令牌的模型
class Invite < ApplicationRecord
before_create :generate_token
def generate_token
self.token = Digest::SHA1.hexdigest([self.user_group_id, Time.now, rand].join)
end
end
不确定路由是否完整?
Rails.application.routes.draw do
resources :memberships
resources :user_groups
# mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
# devise_for :users
# delete '/logout', to: 'sessions#destroy'
default_url_options :host => "smtp.gmail.com"
devise_for :users, controllers: {
sessions: 'users/sessions',
passwords: 'users/passwords',
registrations: 'users/registrations'
}
get 'static_pages/index'
resources :invites
namespace :admin, path: '/admin' do
resources :users, except: [:show] do
member do
post :resend_invite
post :enable
end
end
resources :roles, only: [:index, :edit, :update, :new, :create]
get "audits/index"
end
end
查看表单以邀请新用户和现有用户。
= simple_form_for @invite , :url => invites_path do |f|
= f.error_notification
= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present?
space-left3.space-below3
= f.hidden_field :user_group_id, :value => @invite.user_group_id
= f.input :email, placeholder: 'user@domain.com'
.form-actions
button.btn.btn-primary type="submit" Send
mailer/invite_mailer.rb
class InviteMailer < ApplicationMailer
def self.new_user_invite(invite, signup_url)
subject 'Invite'
recipients invite.recipient_email
from 'example@gmail.com'
body :invite => invite, :signup_url => signup_url
invite.update_attribute(:sent_at, Time.now)
end
def invite
@greeting = "Hi"
mail to: "to@example.org"
end
end
邮件程序/应用程序邮件程序
class ApplicationMailer < ActionMailer::Base
default from: 'from@example.com'
layout 'mailer'
end
发布于 2019-03-05 15:52:48
不知道我是否可以解释它,但是对于邮件程序,我们只定义方法而不使用类语法。把mailer想成控制器很方便,它们非常相似。应该是:
class InviteMailer < ApplicationMailer
def new_user_invite(invite, signup_url)
@signup_url = signup_url
invite.update_attribute(:sent_at, Time.now)
mail(to: invite.email, subject: 'Invite')
end
end
现在,您需要一个电子邮件视图。在app/views/invite_mailer/中创建一个名为new_user_invite.html.erb
的文件。为了简单起见,它将是:
<p>Someone invited you to the project, please click link: @signup_url</p>
此外,您还需要在邮件调用中将new_user_registration_path
更改为new_user_registration_url
,以传递完整链接
您可以在guides中阅读有关邮件程序的更多信息
https://stackoverflow.com/questions/54991357
复制相似问题