我想用rails 5和mongoid创建一个邀请系统。但是,当我想发送电子邮件时,我有这个错误:
Recipient can't be blank
something goes wrong
Recipient can't be blank
No template found for InvitesController#create, rendering head :no_content
Completed 204 No Content in 137ms
以下是代码:
invite.rb
class Invite
include Mongoid::Document
include Mongoid::Timestamps
field :email, type: String
field :token, type: String
belongs_to :project
belongs_to :sender_id, :class_name => 'User', inverse_of: :recipient_id
belongs_to :recipient_id, :class_name => 'User', inverse_of: :sender_id
before_create :generate_token
def generate_token
self.token = Digest::SHA1.hexdigest([self.project_id, Time.now, rand].join)
end
end
project.rb
class Project
include Mongoid::Document
include Mongoid::Timestamps
# Principle informations concerning project
field :title, type: String
field :description, type: String
field :client, type: String
field :deadline, type: String
field :creator_id, type: String
belongs_to :owner, :class_name => 'User', inverse_of: :members
has_many :members, :class_name => 'User', inverse_of: :owner
has_many :invites
end
user.rb
class User
include Mongoid::Document
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
## Database authenticatable
field :email, type: String, default: ""
field :encrypted_password, type: String, default: ""
## Recoverable
field :reset_password_token, type: String
field :reset_password_sent_at, type: Time
## Rememberable
field :remember_created_at, type: Time
## Trackable
field :sign_in_count, type: Integer, default: 0
field :current_sign_in_at, type: Time
field :last_sign_in_at, type: Time
field :current_sign_in_ip, type: String
field :last_sign_in_ip, type: String
has_many :projects, inverse_of: :projects
has_many :invitations, :class_name => 'Invite', :foreign_key => 'recipient_id'
has_many :sent_invites, :class_name => 'Invite', :foreign_key => 'sender_id'
end
在用户页面上发送邀请的表单:
<%= form_for @invite do |f| %>
<%= f.text_field :project_id, :value => @project.id %>
<%= f.label :email %>
<%= f.email_field :email %>
<%= f.submit 'Send' %>
<% end %>
invites_controller.rb
class InvitesController < ApplicationController
def new
@invite = Invite.new
end
def create
@invite = Invite.new(invite_params)
@invite.sender_id = current_user.id
if @invite.save
InviteMailer.new_user_invite(@invite, new_user_registration_path(:invite_token => @invite.token)).deliver
redirect_to user_path(current_user.id)
else
puts @invite.errors.full_messages
puts "something goes wrong"
end
puts @invite.errors.full_messages
end
private
def invite_params
params.require(:invite).permit(:email, :token, :sender_id, :recipient_id, [:project_id])
end
end
我希望所有者能够邀请其他人,如果用户存在,则将邮件发送给现有用户,但如果没有,则发送链接以创建帐户并在项目中注册,
我忘记了什么?为什么我有这个错误?
谢谢!
答案 0 :(得分:5)
在创建操作中,最后您需要呈现页面,否则您需要将用户重定向到另一个页面。例如,像这样更改代码:
def create
@invite = Invite.new(invite_params)
@invite.sender_id = current_user.id
if @invite.save
InviteMailer.new_user_invite(@invite, new_user_registration_path(:invite_token => @invite.token)).deliver
redirect_to user_path(current_user.id)
else
puts @invite.errors.full_messages
puts "something goes wrong"
redirect_to :back #OR render :new
end
end
观察我是否重定向用户,如果保存对象失败(在&#39;其他&#39;块结束时)
如果你想要更好的代码,你应该有这样的东西:
def create
@invite = Invite.new(invite_params)
@invite.sender_id = current_user.id
respond_to do |format|
if @invite.save
format.html { redirect_to '/', notice: 'Invite was successfully created' }
format.json { render :@invite.json }
else
format.html { render :new }
format.json { render json: @invite.errors, status: :unprocessable_entity }
end
end
end
以上代码处理html和json请求
答案 1 :(得分:1)
默认情况下,Rails中的控制器会自动呈现名称与方法名称对应的视图。 create方法中的HttpSecurity http = ...
http.exceptionHandling().accessDeniedPage("/403");
块没有响应,因此rails正在寻找要呈现的名为else
的视图。将create
放在该块中,以便重新呈现render :new
视图。
答案 2 :(得分:0)
更改您的创建方法,如下所示:
def create
@invite = Invite.new(invite_params)
@invite.sender_id = current_user.id
if @invite.save
InviteMailer.new_user_invite(@invite, new_user_registration_path(:invite_token => @invite.token)).deliver
redirect_to user_path(current_user.id)
else
puts @invite.errors.full_messages
puts "something goes wrong"
render :new
end
end