嘿伙计们,当我点击地方订单按钮到用户电子邮件ID时,我想在rails中发送电子邮件但是当我点击按钮undefined method 'items' for #<Order:0x6d83c48>
时出现此错误
这是我的邮件文件夹
class OrderMailer < ActionMailer::Base
default from: "gokuverma94@gmail.com"
def order_confirmation order
@order=order
mail to: order.user.email, subject: "Your order (##{order.id})"
end
end
这是我的orders_controller.rb
class OrdersController < ApplicationController
def create
@order_form=OrderForm.new(user: User.new(order_params[:user]),cart: @cart)
if @order_form.save
notify_user
redirect_to root_path,notice:"Thank You for placing the order sir."
else
render "carts/checkout"
end
end
private
def notify_user
OrderMailer.order_confirmation(@order_form.order).deliver
end
def order_params
params.require(:order_form).permit(
user:[:name,:phone,:address,:city,:country,:postal_code,:email])
end
end
最后是views / order_mailer文件夹中的order_confirmation.text.erb
<h2>Hello</h2><%=@order.user.name%>,
Thank you for placing an order. Here is a summary for order #<%=@order.id%>:
<% @order.items.each do |item|%>
*<%=item.quantity%><%=item.product.name%>($<%=item.total_price%>)
<%end%>
Total: $<%=@order.total_price%>
@ order.user.name工作正常,但是, 我不知道为什么它找不到物品方法的顺序可以有人请告诉我它有什么问题因为我似乎无法弄清楚
cart.rb
class Cart
attr_reader :items
def self.build_from_hash (hash)
items= if hash["cart"] then
hash["cart"]["items"].map do |item_data|
CartItem.new item_data["product_id"],item_data["quantity"]
end
else
[]
end
new items
end
def initialize items=[]
@items=items
end
def add_item (product_id)
item=@items.find { |item| item.product_id == product_id}
if item
item.increment
else
@items << CartItem.new(product_id)
end
end
def empty?
@items.empty?
end
def count
@items.length
end
def grand_total
@items.inject(0){|sum,item| sum+item.total_price}
end
def serialize
items = @items.map do |item|
{
"product_id" =>item.product_id,
"quantity" => item.quantity
}
end
{
"items" => items
}
end
end
order_item.rb
class OrderItem < ApplicationRecord
belongs_to :order
belongs_to :product
def total_price
self.product.price*self.quantity
end
end
cart_item.rb
class CartItem
attr_reader :product_id,:quantity
def initialize (product_id,quantity=1)
@product_id=product_id
@quantity=quantity
end
def increment
@quantity=@quantity+1
end
def product
Product.find product_id
end
def total_price
product.price*quantity
end
end
答案 0 :(得分:0)
从您的模型数据订单中没有项目,因此您无法使用@order.items
将<% @order.items.each do |item|%>
替换为<% @order.order_items.each do |item|%>