2014-12-31 62 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 
相關問題