Rails Order 的未定义局部变量或方法“current_user”
Rails undefined local variable or method `current_user' for Order
您好,当用户结账时,我的网站出现了问题。创建或登录用户时,所有用户都有一个购物车。应用程序控制器中包含的会话助手中有一个 current_user 实例。当用户将商品放入购物车后去下订单时,我看到错误 "undefined local variable or method 'current_user' for #" This is the code that is in the Order model
def add_line_items_from_cart(cart)
current_user.cart.line_items each do |item|
item.cart_id = nil
line_items << item
end
我对此有点困惑,因为我认为 current_user.cart 会起作用。
这是我的订单控制器
class OrdersController < ApplicationController
include CurrentCart
before_action :set_cart, only: [:new, :create]
before_action :set_order, only: [:show, :edit, :update, :destroy]
def new
if current_user.cart.line_items.empty?
redirect_to root_path, notice: "Your cart is empty"
return
end
@order = Order.new
end
def create
@order = Order.new(order_params)
@order.add_line_items_from_cart(@cart)
respond_to do |format|
if @order.save
current_user.cart.destroy
session[:cart_id] = nil
format.html { redirect_to root_path, notice:
'Thank you for your order.'}
format.json { render action: 'show', status: :created,
location: @order}
else
format.html {render action: 'new'}
format.json {render json: @order.errors,
status: :unprocessable_entity}
end
end
end
private
def order_params
params.require(:order).permit(:name, :address, :email, :pay_type)
end
end
我的订单部分用于用户何时可以下订单
<%= form_for(@order) do |f|%>
<% if @order.errors.any?%>
<div id="error_explanation">
<h2> <%= pluralize(@order.errors.count, "error")%>
prohibited this order from being saved:</h2>
<ul>
<% @order.errors.full_messages.each do |msg| %>
<li><%= msg%></li>
<%end%>
</ul>
</div>
<%end%>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name, size: 40%>
</div>
<div class="field">
<%= f.label :address %><br>
<%= f.text_area :address, rows: 3, cols: 40%>
</div>
<div class="field">
<%= f.label :email %><br>
<%= f.email_field :email, size: 40%>
</div> </div>
<div class="field">
<%= f.label :pay_type %><br>
<%= f.select :pay_type, Order::PAYMENT_TYPES,
prompt: 'Select a payment method'%>
</div>
<div class="actions">
<%= f.submit 'Place Order' %>
</div>
<%end%>
如果有人能向我解释为什么这不起作用,那将非常有帮助。
Theres an instance of current_user in a sessions helper thats included in the applications controller.
您的模型层中没有任何部分可用。您的模型无法访问控制器变量或助手。你不能在你的模型层中使用 current_user
除非你特别将它作为参数传递给你所在的任何方法。
您好,当用户结账时,我的网站出现了问题。创建或登录用户时,所有用户都有一个购物车。应用程序控制器中包含的会话助手中有一个 current_user 实例。当用户将商品放入购物车后去下订单时,我看到错误 "undefined local variable or method 'current_user' for #" This is the code that is in the Order model
def add_line_items_from_cart(cart)
current_user.cart.line_items each do |item|
item.cart_id = nil
line_items << item
end
我对此有点困惑,因为我认为 current_user.cart 会起作用。 这是我的订单控制器
class OrdersController < ApplicationController
include CurrentCart
before_action :set_cart, only: [:new, :create]
before_action :set_order, only: [:show, :edit, :update, :destroy]
def new
if current_user.cart.line_items.empty?
redirect_to root_path, notice: "Your cart is empty"
return
end
@order = Order.new
end
def create
@order = Order.new(order_params)
@order.add_line_items_from_cart(@cart)
respond_to do |format|
if @order.save
current_user.cart.destroy
session[:cart_id] = nil
format.html { redirect_to root_path, notice:
'Thank you for your order.'}
format.json { render action: 'show', status: :created,
location: @order}
else
format.html {render action: 'new'}
format.json {render json: @order.errors,
status: :unprocessable_entity}
end
end
end
private
def order_params
params.require(:order).permit(:name, :address, :email, :pay_type)
end
end
我的订单部分用于用户何时可以下订单
<%= form_for(@order) do |f|%>
<% if @order.errors.any?%>
<div id="error_explanation">
<h2> <%= pluralize(@order.errors.count, "error")%>
prohibited this order from being saved:</h2>
<ul>
<% @order.errors.full_messages.each do |msg| %>
<li><%= msg%></li>
<%end%>
</ul>
</div>
<%end%>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name, size: 40%>
</div>
<div class="field">
<%= f.label :address %><br>
<%= f.text_area :address, rows: 3, cols: 40%>
</div>
<div class="field">
<%= f.label :email %><br>
<%= f.email_field :email, size: 40%>
</div> </div>
<div class="field">
<%= f.label :pay_type %><br>
<%= f.select :pay_type, Order::PAYMENT_TYPES,
prompt: 'Select a payment method'%>
</div>
<div class="actions">
<%= f.submit 'Place Order' %>
</div>
<%end%>
如果有人能向我解释为什么这不起作用,那将非常有帮助。
Theres an instance of current_user in a sessions helper thats included in the applications controller.
您的模型层中没有任何部分可用。您的模型无法访问控制器变量或助手。你不能在你的模型层中使用 current_user
除非你特别将它作为参数传递给你所在的任何方法。