2016-08-04 41 views
1

我有這些時刻之一。我的問題可能很簡單,但我沒有弄清楚。Rails 5表格中的聯繫人表單數據

我有一個我正在開發的Rails 5應用程序。它包含聯繫人填寫和提交的聯繫表單。

接下來,我將ActionMailer設置爲向聯繫人發送電子郵件,通知他們他們的郵件已收到並正在接受審閱。我也有ActionMailer設置發送電子郵件到網站管理員,應該包含聯繫人表單數據。這是我遇到問題的地方。如何將聯繫表單數據傳遞到我的電子郵件視圖?

這裏是contacts_controller:

class ContactsController < ApplicationController 
    before_action :set_contact, only: [:show, :edit, :update, :destroy] 

    # GET /contacts 
    # GET /contacts. 

    def index 
    @contacts = Contact.all 
    end 

    # GET /contacts/1 
    # GET /contacts/1.json 
    def show 
    end 



    # GET /contacts/new 
    def new 
    @contact = Contact.new 
    end 

    # GET /contacts/1/edit 
    def edit 
    end 

    # POST /contacts 
    # POST /contacts.json 
    def create 
    @contact = Contact.new(contact_params) 

    respond_to do |format| 
     if @contact.save 


     FormMailer.message_received(@contact).deliver_now 
     FormMailer.new_web_message(@email).deliver_now 
     format.html { redirect_to thank_you_path, notice: 'Your message was sent.' } 
     format.json { render :show, status: :created, location: @contact } 
     else 
     format.html { render :new } 
     format.json { render json: @contact.errors, status: :unprocessable_entity } 
     end 
    end 
    end 



    # PATCH/PUT /contacts/1 
    # PATCH/PUT /contacts/1.json 
    def update 
    respond_to do |format| 
     if @contact.update(contact_params) 
     format.html { redirect_to @contact, notice: 'Contact was successfully updated.' } 
     format.json { render :show, status: :ok, location: @contact } 
     else 
     format.html { render :edit } 
     format.json { render json: @contact.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /contacts/1 
    # DELETE /contacts/1.json 
    def destroy 
    @contact.destroy 
    respond_to do |format| 
     format.html { redirect_to contacts_url, notice: 'Contact was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_contact 
     @contact = Contact.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def contact_params 
     params.require(:contact).permit(:first_name, :last_name, :vehicle_type, :tire_size, :current_tire, :phone_number, :email, :message) 
    end 
end 

的梅勒:

class FormMailer < ApplicationMailer 

    # Subject can be set in your I18n file at config/locales/en.yml 
    # with the following lookup: 
    # 
    # en.form_mailer.message_received.subject 
    # 
    def message_received(contact) 
    @contact = contact 
    @url = 'http://example.com' 
    @greeting = "Hello" 


    mail(to: @contact.email, subject: 'Thanks for contacting Halstead Tire LLC') 

    end 
    def new_web_message 


    @greeting = "Hello" 
    @email = '<[email protected]>' 
    @website = 'http://www.example.com' 



    mail(to: @email, subject: 'A new message from your website.') 
    end 



end 

視圖new_web_message.html.erb:

<h1><%= @greeting %> Nathan,</h1> 

<p> 
    Someone's requested some information from your site. Please go to <%= @website %> to get the details. 
</p> 
<body> 

</body> 

我有電子郵件發送正確的,他們是按預期交付。聯繫人的消息正在完美工作。我想讓Admin *(new_web_message)*電子郵件接收聯繫人的信息併發送。我如何將這些值傳遞給視圖?我想將所有contact_params *(來自contacts_controller)*並將它們放入電子郵件中的管理員。我意識到這可能是一個簡單而愚蠢的錯誤,但我無法弄清楚。是否有可能在一個郵件程序中完成這一切,或者我是否需要爲聯繫人和管理員*(new_web_message)*提供單獨的郵件程序? *正如我所說的,現在郵件工作正常,但是我只需要將表單中的數據傳遞給管理員**(new_web_message)*電子郵件。

在電子郵件聯繫它的工作訪問,像這樣的數據是在郵件很明顯:

@contact.email 
@contact.phone_number 
@contact.message 

等等。爲什麼在管理員電子郵件中這種理論不起作用?

我的郵件程序是否需要與控制器(contacts_mailer)相同,而不是(FormMailer),因爲我現在命名它?

謝謝!

編輯:

所以我實現了瑞恩的代碼在我的應用程序,但現在我得到:

NoMethodError in Contacts#create 

這裏是new_web_message.html.erb

<h1><%= @greeting %> Nathan,</h1> 

<p> 
    Someone's requested some information from your site. Please go to <%=  @website %> to get the details. 
</p> 
<body> 
<p><%= @contact.first_name %></p> 
<p><%= @contact.last_name %></p> 

</body> 

這裏顯示的錯誤當我提交表格時:

Showing /Users/evan/htire/app/views/form_mailer/new_web_message.html.erb where line #7 raised: 

undefined method `first_name' for nil:NilClass 
Extracted source (around line #7): 


</p> 
<body> 
<p><%= @contact.first_name %></p> (line 7) 
<p><%= @contact.last_name %></p> (line 8) 

</body> 

顯然我需要在某處定義方法,但我正在用盡想法。 (是的,我有點新的軌道)

這是模型聯繫。使用瑞安的建議RB

class Contact < ApplicationRecord 

    def new_web_message 
     first_name = first_name 
     last_name = last_name 

    end 


     attr_accessor :first_name, :last_name, :vehicle_type, :tire_size, :current_tire, :phone_number, :email, :message 



    validates_presence_of :first_name 
    validates_presence_of :last_name 
    validates_presence_of :phone_number 
    validates_presence_of :message 

end 

更新郵件:

class FormMailer < ApplicationMailer 

    # Subject can be set in your I18n file at config/locales/en.yml 
    # with the following lookup: 
    # 
    # en.form_mailer.message_received.subject 
    # 
    def message_received(contact) 
    @contact = contact 
    @url = 'http://halsteadtire.net' 
    @greeting = "Hello" 


    mail(to: @contact.email, subject: 'Thanks for contacting Halstead Tire LLC') 

    end 
    def new_web_message(contact, params) 
    @contact = contact 

    @greeting = "Hello" 
    @email = '<[email protected]>' 
    @website = 'http://www.halsteadtire.net/contacts' 


    mail(to: @email, subject: 'A new message from your website.') 
    end 



end 

使用瑞安的代碼更新控制器:

class ContactsController < ApplicationController 
    before_action :set_contact, only: [:show, :edit, :update, :destroy] 

    # GET /contacts 
    # GET /contacts.json 
    def index 
    @contacts = Contact.all 
    end 

    # GET /contacts/1 
    # GET /contacts/1.json 
    def show 
    end 



    # GET /contacts/new 
    def new 
    @contact = Contact.new 
    end 

    # GET /contacts/1/edit 
    def edit 
    end 

    # POST /contacts 
    # POST /contacts.json 
    def create 
    @contact = Contact.new(contact_params) 

    respond_to do |format| 
     if @contact.save 


     FormMailer.message_received(@contact).deliver_now 
     FormMailer.new_web_message(@email, params).deliver_now 
     format.html { redirect_to thank_you_path, notice: 'Your message was sent.' } 
     format.json { render :show, status: :created, location: @contact } 
     else 
     format.html { render :new } 
     format.json { render json: @contact.errors, status: :unprocessable_entity } 
     end 
    end 
    end 



    # PATCH/PUT /contacts/1 
    # PATCH/PUT /contacts/1.json 
    def update 
    respond_to do |format| 
     if @contact.update(contact_params) 
     format.html { redirect_to @contact, notice: 'Contact was successfully updated.' } 
     format.json { render :show, status: :ok, location: @contact } 
     else 
     format.html { render :edit } 
     format.json { render json: @contact.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /contacts/1 
    # DELETE /contacts/1.json 
    def destroy 
    @contact.destroy 
    respond_to do |format| 
     format.html { redirect_to contacts_url, notice: 'Contact was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_contact 
     @contact = Contact.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def contact_params 
     params.require(:contact).permit(:first_name, :last_name, :vehicle_type, :tire_size, :current_tire, :phone_number, :email, :message) 
    end 
end 

我在做什麼錯?

回答

0

您應該可以將參數傳遞到new_web_message。你的方法更改爲:

def new_web_message(contact) 
    @contact = contact 
    #rest of your code 
end 

然後調用使用此:

FormMailer.new_web_message(@contact).deliver_now 

如果你想把所有的PARAMS(即即使你不保存到Contact的那些),只需將其添加爲附加PARAM:

def new_web_message(contact, params) 
    ... 
end 

而且

FormMailer.new_web_message(@email, params).deliver_now 

聲明:我不知道這是否適用於Rails 5(它應該),但這可以在Rails 4.2中使用。