2014-12-31 2 views
0

Я пытаюсь создать почтовый ящик в моем приложении rails, что позволяет посетителям отправлять отзывы через форму, которая отправляется на адрес электронной почты третьей стороны. Этот объект определяется как «комментарий».Как передать пользовательские атрибуты в действие mailer

Как часть приложения, пользователь должен пройти аутентификацию, чтобы получить доступ к этой форме и отправить мне «комментарий», и поэтому я пытаюсь передать атрибуты из модели пользователя в форму комментариев и почтовую программу ,

Я получаю следующее сообщение об ошибке, код следовать:

неопределенный метод `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.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 
Смежные вопросы