Ruby on rails - 没有找到InvitesController #create的模板

Ant*_*chd 0 ruby ruby-on-rails

我想用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
Run Code Online (Sandbox Code Playgroud)

这是代码:

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

在用户页面上发送邀请的表单:

<%= form_for @invite do |f| %>
            <%= f.text_field :project_id, :value => @project.id %>
            <%= f.label :email %>
            <%= f.email_field :email %>
            <%= f.submit 'Send' %>
        <% end %>
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

我希望所有者能够邀请其他人,如果用户存在,则将邮件发送给现有用户,但如果没有,则发送链接以创建帐户并在项目中注册,

我忘记了什么?为什么我有这个错误?

谢谢 !

Abh*_*ddy 5

在create action中,最后需要呈现页面,否则需要将用户重定向到另一个页面.例如,像这样更改代码:

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
Run Code Online (Sandbox Code Playgroud)

如果保存对象失败,请注意我正在重定向用户(在'else'块的末尾)

如果你想要更好的代码,你应该有这样的东西:

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
Run Code Online (Sandbox Code Playgroud)

上面的代码处理html和json请求