RoR:InviteMailer:Class

时间:2019-03-04 20:52:19

标签: ruby-on-rails ruby ruby-on-rails-4

我使用了以下有关创建范围内邀请(https://coderwall.com/p/rqjjca/creating-a-scoped-invitation-system-for-rails)的教程来创建邀请,用户,组,但遇到上述错误,不确定如何解决。请记住,我仍在学习RoR。谢谢。

我在Invitations_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

邀请发送令牌的模型

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

mailer / application_mailer

 class ApplicationMailer < ActionMailer::Base
      default from: 'from@example.com'
      layout 'mailer'
    end

2 个答案:

答案 0 :(得分:1)

不确定我是否可以解释它,但是对于邮件,我们只定义没有类语法的方法。像控制器一样思考邮件很方便,它们非常相似。应该是:

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

中了解有关邮件的更多信息。

答案 1 :(得分:0)

要在new_user_invite上将InviteMailer创建为类方法,请执行以下操作:

class InviteMailer < ApplicationMailer

  class << self 

    def new_user_invite(invite, path)
      ... do stuff
    end

  end

  def invite
    @greeting = "Hi"
    mail to: "to@example.org"
  end

end

或者

class InviteMailer < ApplicationMailer

  def self.new_user_invite(invite, path)
    ... do stuff
  end

  def invite
    @greeting = "Hi"
    mail to: "to@example.org"
  end

end

您使用哪种个人偏好设置。

我想指出Jörg W Mittag想说:

  

我是那些想要指出Ruby中没有类方法之类的Ruby Purists的人之一。不过,我很好地使用了 class方法这个术语,只要各方都完全理解这是一个俗语用法。换句话说,如果您知道,就没有类方法之类的东西,并且术语“类方法”只是“作为实例的对象的单例类的实例方法”的简称的Class”,那么就没有问题。但是否则,我只会看到它妨碍理解。

让所有各方都完全理解,术语“ <类>类方法” 在上面用的是口语含义。