在Rails中发送电子邮件的按钮

时间:2018-08-08 22:26:09

标签: ruby-on-rails ruby ruby-on-rails-5

我试图在用户显示页面上添加一个按钮,以在用户表中提到的用户地址上发送电子邮件。当我单击“发送邀请电子邮件”时,我遇到了错误 undefined method`email '代表nil:NilClass 。请让我知道我正在做的错误。

routes.rb

order by

invitation_confirmation.text.erb

get 'send_invitation_email', to: 'users#invitation_confirmation'

user_mailer.rb

Hi,

Thank you for setting up an account. Please click on the link in that email to start using the system. 

<%= edit_password_reset_url(@user.reset_token, email: @user.email) %>

users_controller.rb

def invitation_confirmation(user)
    @user = user
    mail(:to => "<#{user.email}>", subject: "Notification")
end 

show.html.erb

def invitation_confirmation
    @user = User.find(params[:id])  
    UserMailer.invitation_confirmation(@user).deliver
    flash[:notice] = "Invitation email has been sent."
    redirect_to user_path(@user)
end

更新

<% if logged_in? && current_user.admin? || current_user.superadmin? %>
   <%= link_to "Send Invitation Email", send_invitation_email_path(@user), class: "btn btn-xs btn-primary" %>
<% end %>

4 个答案:

答案 0 :(得分:0)

该错误表明该用户在您的UserMailer.invitation_confirmation方法中为零。

尝试:

def invitation_confirmation(user)
    @user = user
    mail(:to => "<#{@user.email}>", subject: "Notification")
end 

答案 1 :(得分:0)

尝试一下

def invitation_confirmation(user)
  mail(to: user.email, subject: "Notification")
end

答案 2 :(得分:0)

如果@user对象不为null,只需将show.html.erb的代码更改为此:

<% if logged_in? && current_user.admin? || current_user.superadmin? %>
  <%= link_to "Send Invitation Email", send_invitation_email_path(id: @user.id), class: "btn btn-xs btn-primary" %>
<% end %>

答案 3 :(得分:0)

def invitation_confirmation
    @user = User.find(params[:format]) 
    **@user.create_reset_digest**   
    UserMailer.invitation_confirmation(@user).deliver
    flash[:notice] = "Invitation email has been sent."
    redirect_to user_path(@user)
end

我在粗体字行中犯了一个错误。我还没有created_reset_digest。现在正在工作。谢谢大家。