Ruby on rails 如果顺序为零,则重定向或呈现页面
如果找不到订单,我将尝试渲染。页面未重定向,我收到错误信息: 找不到订单,未定义nil:NilClass的“shopify_name”方法 我的看法是:Ruby on rails 如果顺序为零,则重定向或呈现页面,ruby-on-rails,ruby,Ruby On Rails,Ruby,如果找不到订单,我将尝试渲染。页面未重定向,我收到错误信息: 找不到订单,未定义nil:NilClass的“shopify_name”方法 我的看法是: <h1>Cancel Your Order Here</h1> <h3 style="color: white;">YOUR ORDER NUMBER IS <%= @order.shopify_name %></h3> <div id="multiple-f
<h1>Cancel Your Order Here</h1>
<h3 style="color: white;">YOUR ORDER NUMBER IS <%= @order.shopify_name %></h3>
<div id="multiple-file-preview">
<div class="card-body">
<% if @order.cancelled? %>
<h5>YOUR ORDER HAS BEEN CANCELLED SUCCESSFULLY!</b> <br><br>Please contact our customer service for further information on <b>info@mrswordsmith.com</b></h5>
<% elsif @order.scheduled? %>
<%= link_to('Cancel',
order_path(@order.shopify_id), class: 'btn btn-danger',
data: {
disable_with: "Order Cancelled"
}, method: :delete)
%>
<% else %>
<h5>As you are attempting to cancel outside of the 2 hour cancellation window, please contact our customer support team</b></h5>
<% end %>
当我试图更改url中的订单号时,它给了我一个错误提示:
`shopify_name'代表nil:NilClass。我想知道如何在屏幕上渲染
同一页
我不完全确定你想做什么,但是 如果您希望在@order存在时呈现“取消”视图,否则重定向,则控制器操作将如下所示:
def cancel
@order = Order.find_by(shopify_id: params[:id])
unless @order
flash[:alert] = 'Order not found'
redirect_to root_path # or wherever you want them to go if order doesn't exist
end
# render will happen automatically, is default behavior
end
def cancel
@order = Order.find_by(shopify_id: params[:id])
unless @order
flash.now[:alert] = 'Order not found'
render :new # render the new view with the cancel url
end
end
如果要在找不到顺序的情况下使用相同的URL呈现不同的视图,可以执行以下操作:
def cancel
@order = Order.find_by(shopify_id: params[:id])
unless @order
flash[:alert] = 'Order not found'
redirect_to root_path # or wherever you want them to go if order doesn't exist
end
# render will happen automatically, is default behavior
end
def cancel
@order = Order.find_by(shopify_id: params[:id])
unless @order
flash.now[:alert] = 'Order not found'
render :new # render the new view with the cancel url
end
end
您需要在控制器中渲染或重定向,@order正在设置。@dinjas嗨,我已经更新了我的cotrollerhi@dinjas谢谢您的回答,这真的很有帮助!如果我想在同一页上渲染,如果我能做些什么呢?我不知道你说的在同一页上渲染是什么意思。您可以添加一个条件。。。。。。。这就是你的意思吗?