我想用rails 5和mongoid創建一個邀請系統。但是,當我想發送的電子郵件,我有此錯誤:Ruby on rails - 找不到InvitesController的模板#create
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
我希望主人能邀請其他人,如果用戶存在,則將郵件發送給現有用戶,但如果不存在,則發送鏈接以創建帳戶和註冊表ter在項目中,
我忘了什麼?爲什麼我有這個錯誤?
謝謝!