2015-08-21 28 views
0

我使用rails 4和actionmailer允許用戶在發送郵件之前編輯生成的電子郵件。嘗試編輯電子郵件的Rails/Actionmailer錯誤

當我嘗試加載編輯頁面時,出現此錯誤。

Showing /var/www/rqm3/app/views/rfis/mail.html.erb where line #5 raised: 
undefined method `body' for nil:NilClass 

這裏介紹第5行。

<%= text_area_tag :email_body, @mail_message.html_part.body.raw_source,class:"tinymce", rows:40, cols:120 %> 

我在這裏設置了我的控制器的@mail_message。

def mail 
    @mail_message = RfiMailer.send_rfi(current_user, @rfi) 
    end 

感謝任何人的幫助。

編輯:

rfis_controller:

class RfisController < ApplicationController 
    before_action :set_rfi, only: [:show, :edit, :update, :destroy, :mail] 
    before_action :authenticate_user! 
    # GET /rfis 
    # GET /rfis.json 
    def index 
    @rfis = Rfi.all 
    end 

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

    # GET /rfis/new 
    def new 
    @rfi = Rfi.new 
    end 

    # GET /rfis/1/edit 
    def edit 
    end 

    def send_rfi 

    end 

    def mail 
    @mail_message = RfiMailer.send_rfi(current_user) 
    # @mail_message = RfqMailer.placeholder_message(current_user, Rfq.last) 
    end 

    # POST /rfis 
    # POST /rfis.json 
    def create 
    @rfi = Rfi.new(rfi_params) 

    respond_to do |format| 
     if @rfi.save 
     format.html { redirect_to mail_rfi_url(@rfi), notice: 'Rfi was successfully created.' } 
     format.json { render :show, status: :created, location: @rfi } 
     else 
     format.html { render :new } 
     format.json { render json: @rfi.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

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

    # DELETE /rfis/1 
    # DELETE /rfis/1.json 
    def destroy 
    @rfi.destroy 
    respond_to do |format| 
     format.html { redirect_to rfis_url, notice: 'Rfi was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

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

    # Never trust parameters from the scary internet, only allow the white list through. 
    def rfi_params 
     params.require(:rfi).permit(:due, :rfi_type, :parties, :reference, :svg_ref, :vendor_ref, :email_body) 
    end 
end 

rfi_mailer.rb

class RfiMailer < ApplicationMailer 
    default from:"[email protected]" 
    def send_rfi(user) 
     mail(to:"someemail", subject:"test") 
    end 
end 
+0

好嗎首先:你缺少使用用戶編輯模板在你的郵件。 –

+0

現在我只是想讓電子郵件顯示在「郵件」視圖的TinyMCE中。在過去,我做了這樣的事情: @ mail_message.html_part.body.raw_source.replace params [:email_body] – Suavocado

+1

因此,在您創建一些「RFI」後,您想要轉到「郵件」方法 - 您在其中顯示textarea。正確? –

回答

1

首先,一些清理

before_action :set_rfi, only: [:show, :edit, :update, :destroy, :mail] 

你也可以寫

​​

返回主題

html_part是零,這就是爲什麼你不能用它的身體,它拋出一個異常。 請確保您的電子郵件有2個模板,一個用於文本部分,另一個用於html部分。再在其上的細節在軌文件

http://guides.rubyonrails.org/action_mailer_basics.html#mailer-views

+0

感謝噸,我沒有意識到這會有所作爲。 – Suavocado

+1

其實我不知道我能幫上什麼忙,但如果現在工作了,我很高興。讓我知道你是否需要進一步的協助 –