简体   繁体   中英

How to pass user attributes to action mailer

I'm trying to build a mailer into my rails app, which allows visitors to submit feedback through a form which is sent to a third party email address. This object is defined as a 'comment'.

As part of the app, a user must be authenticated in order to access this form and send me a 'comment', and as such I'm trying to pass the attributes from the User model to the comment form and mailer.

I'm getting the following error, code to follow:

undefined method `user_full_name' for nil:NilClass

  <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
  Name: <%= @comment.user_full_name %>

  Email: <%= @comment.user_email %>

routes.rb

Sampleapp::Application.routes.draw do

  devise_for :users, :controllers => { :registrations => "registrations" }

  devise_scope :user do
    get 'register', to: 'devise/registrations#new'
    get 'login',    to: 'devise/sessions#new',     as: :login
    get 'logout',   to: 'devise/sessions#destroy', as: :logout
  end

  resources :users do

    member do
      get 'edit_profile'
    end
  resources :messages, only: [:new, :create]
  end

  resources :messages do
    get :sent, action: "outbox", type: "sent", on: :collection
  end

  resources :comments, only: [:new, :create]

  root to: "home#index"
  match '/about',   to: 'static_pages#about',   via: 'get'
  match '/help',    to: 'static_pages#help',    via: 'get'
  match '/privacy_policy', to: 'static_pages#privacy_policy', via: 'get' 
  match '/terms_and_conditions',   to: 'static_pages#terms_and_conditions',   via: 'get'

end

CommentsController.rb

class CommentsController < ApplicationController

  def new
    @comment = Comment.new
  end

  def create
    @comment = Comment.new comment_params

    if @comment.valid?
      CommentsMailer.new_comment(@user).deliver
      flash[:success] = "We have receieved your message!"
      redirect_to users_path
    else
      flash.now.alert = "Please fill in all fields."
      render :new
    end
  end

  private

  def comment_params
    params.require(:comment).permit(:subject, :feedback)
  end

end

comment.rb

class Comment < ActiveRecord::Base
  belongs_to :user, inverse_of: :comments

  attr_accessible :subject, :feedback 

  validates :subject, presence: true
  validates :feedback, presence: true

end

new.html.erb

<div class="comment">
  <div class="container">
    <%= form_for @comment do |f| %>

      <%= f.hidden_field :user_full_name, value: current_user.full_name %> 
      <%= f.hidden_field :user_email, value: current_user.email %>

      <%= f.label :subject %>
      <%= f.text_field :subject %>

      <%= f.label :feedback %>
      <%= f.text_area :feedback %>

      <%= f.submit "Send", class: "btn btn-inverse" %>
    <% end %>
  </div>
</div>

comments.mailer.rb

class CommentsMailer < ActionMailer::Base
  default to:   "myemail@gmail.com"
  default from: "@comment.user.email"

  def new_comment(user)
    @user = user
    mail(subject: '@comment.subject')
  end

end

new_comment.html.erb

<!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    Name: <%= @comment.user.full_name %>

    Email: <%= @comment.user.email %>

    Subject: <%= @comment.subject %>

    Feedback: <%= @comment.body %>
  </body>
</html>

You are not passing @comment object to CommentsMailer and trying to access it there.

Instead of passing User object ( @user ) to CommentsMailer in create action CommentsMailer.new_comment(@user).deliver . Try pass @comment object.

Try make following changes in your create action :

def create
  @comment = Comment.new comment_params

  if @comment.valid?
    CommentsMailer.new_comment(@comment).deliver
    flash[:success] = "We have receieved your message!"
    redirect_to users_path
  else
    flash.now.alert = "Please fill in all fields."
    render :new
  end
end

comments.mailer.rb

class CommentsMailer < ActionMailer::Base
  default to:   "myemail@gmail.com"
  default from: "@comment.user.email"

  def new_comment(comment)
    @comment = comment
    @user = @comment.user
    mail(subject: '@comment.subject')
  end

end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM