2014-12-31 31 views
0

我试图在我的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: "[email protected]" 
    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> 

回答

0

你是不是将@comment对象传递给CommentsMailer并试图在那里访问它。

而不是将用户对象(@user)传递给CommentsMailer创建操作CommentsMailer.new_comment(@user).deliver。尝试通过@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: "[email protected]" 
    default from: "@comment.user.email" 

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

end 
相关问题