我使用rails 4和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:"test@test.com"
def send_rfi(user)
mail(to:"someemail", subject:"test")
end
end
答案 0 :(得分:1)
首先,进行一些清理
before_action :set_rfi, only: [:show, :edit, :update, :destroy, :mail]
你也可以写
before_action :set_rfi, except: [:index, :new]
返回主题
html_part为零,这就是为什么你不能使用它的主体而它会引发异常。 请确保您的电子邮件有2个模板,一个用于文本部分,另一个用于html部分。有关详细信息,请参阅rails Doc
http://guides.rubyonrails.org/action_mailer_basics.html#mailer-views