0

我試圖訪問以下郵件中的賣家名稱(seller.name)。 Resa是賣家的子女(賣家has_many resas和Resa屬於賣家)。從Rails 4中的Mailer訪問屬性

應用程序/郵寄者/ resa_mailer.rb

class ResaMailer < ActionMailer::Base 
    default from: '[email protected]' 
    def request_mail(resa) 
    @email = resa.request_email 
    @maturity_date = resa.maturity_date 
    @amount = resa.amount 
    @currency = resa.currency 
    @request_expiry_date = resa.request_expiry_date 
    @description = resa.description 
    @seller = Seller.where(["seller_id = resa.seller_id"]) 
    mail to: @email, subject: @seller.name+' requests a deposit' 
    end 
end 

這被稱爲在resas_controller#創建

class ResasController < ApplicationController 
before_action :set_resa, only: [:show, :edit, :update, :destroy] 
before_action :authenticate_seller! 

def new 
    @resa = current_seller.resas.new 
end 

def create 
    @resa = current_seller.resas.new(resa_params) 
    @resa.seller_id = current_seller.mp_id 
    respond_to do |format| 
    if @resa.save 
     ResaMailer.request_mail(@resa).deliver 
     format.html { redirect_to @resa, notice: 'Resa was successfully created.' } 
     format.json { render action: 'show', status: :created, location: @resa } 
    else 
     format.html { render action: 'new' } 
     format.json { render json: @resa.errors, status: :unprocessable_entity } 
    end 
    end 
end 

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

    # Never trust parameters from the scary internet, only allow the white list through. 
    def resa_params 
    params.require(:resa).permit(:resa_date, :maturity_date, :amount, :currency, :request_expiry_date, :request_status, :maturity_status, :description, :seller_id, :buyer_id, :request_email) 
    end 

回答

1

您的代碼有點糊塗:

@seller = Seller.where(["seller_id = resa.seller_id"]).name 
puts @seller.inspect 
puts @seller 
puts @seller.name 

顯示@seller的第一個定義已經是名稱(Seller.where([...])。name)。然後,您嘗試再次調用@ seller.name,並獲取錯誤,因爲您正嘗試對字符串調用「name」方法,因爲它已經是該名稱。

+0

感謝您的回覆。我刪除了3個投入並且錯誤消失了,但是沒有名稱(即「Bill」),我得到了「賣方」。 任何想法? – Dwidoo

+0

沒有足夠的聲望尚未被允許upvote – Dwidoo