如何将用户属性传递给动作邮件程序

时间:2014-12-31 08:54:17

标签: ruby-on-rails actionmailer

我正在尝试在我的rails应用中构建邮件,这样访问者就可以通过发送到第三方电子邮件地址的表单提交反馈。此对象被定义为'评论'。

作为应用程序的一部分,用户必须通过身份验证才能访问此表单并向我发送评论,因此我尝试从用户模型传递属性评论表和邮寄。

我收到以下错误,代码如下:

未定义的方法`user_full_name'为零: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>

1 个答案:

答案 0 :(得分:0)

您没有将@comment对象传递给CommentsMailer并尝试在那里访问它。

而不是在创建操作@user中将User对象(CommentsMailer.new_comment(@user).deliver)传递给CommentsMailer。尝试传递@comment对象。

尝试在创建操作中进行以下更改:

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